2

我在 UI 上创建了一个 jTable,我想根据其他单元格的布尔状态(复选框)将单元格属性从可编辑更改为不可编辑。我一直在查看几个示例,但未能达到预期的目的,主要是因为我犯了使用 NetBeans 创建 UI 的错误,因此创建了大量我什至无法编辑的代码。

我的表:表 http://freepicupload.com/images/337jtable1.png


编辑:固定,工作,代码如下。

生成表/模型的代码:

jTable1.setModel(new MyTableModel());

表模型和实现的逻辑:

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Job Type",
                                    "Name",
                                    "avg Time",
                                    "Buffer",
                                    "Buffer Parts",
                                    "Color"};
    private Object[][] data = {
    {"1", "Station 1",
     new Integer(10), new Boolean(false), new Integer(0), Color.red},
    {"2", "Station 2",
     new Integer(10), new Boolean(false), new Integer(0), Color.blue},
    {"3", "Station 3",
     new Integer(10), new Boolean(false), new Integer(0), Color.green},
    {"4", "Station 4",
     new Integer(10), new Boolean(false), new Integer(0), Color.orange},
    {"5", "Station 5",
     new Integer(10), new Boolean(false), new Integer(0), Color.black}
    };

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 0) { return false; }
        else if (col == 4) { 
            /*if (getValueAt(row,(col-1)) == "false") { System.out.println("NAO PODES EDITAR BOI"); }
            else if (getValueAt(row,(col-1)) == "true") { System.out.println("Podes que eu deixo!"); } */
            boolean di = (Boolean) getValueAt(row,(col-1));
            if (!di) { return false; }
            else { return true; }
        }
        else { return true; }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

在这种情况下,如果第 3 列的复选框被选中(状态为真),它将只允许编辑第 4 列上的单元格。希望能帮助到你!

4

1 回答 1

4

GUI 编辑器默认为您创建一个,但您可以通过指定相关属性DefaultTableModel从生成的代码中撬开模型,如下所示一旦您控制了模型,您就可以根据需要或如此处所示进行覆盖。Custom codeisCellEditable()

图片

于 2012-07-17T17:21:15.840 回答