2

I have created a jTable on an UI, where I want to change a cell attribute from editable to not editable according to other cell's boolean state (checkbox). I've been looking through several examples but failed to do what intended mainly because I made the mistake of creating the UI with NetBeans thus creating ton's of code which I can't even edit.

My Table: table http://freepicupload.com/images/337jtable1.png!


EDIT: FIXED, working, code below.

Code generating the table/model:

jTable1.setModel(new MyTableModel());

Table Model and the logic implemented:

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("--------------------------");
    }
}

In this case, it will only allow to edit cells on column 4 if column 3's checkbox is checked (state true). Hope it helps!

1 Answers1

4

The GUI editor creates a DefaultTableModel for you by default, but you can pry the model loose from the generated code by specifying Custom code for the relevant property, as shown here. Once you have control of the model, you can override isCellEditable() as required or as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    That I didn't know! Thank you very much, I will try and look at some examples to try and write a model that does what I need! Thank you! – Luis Afonso Teixeira Jul 17 '12 at 17:29
  • I was able to use a custom model, but I came across with two small problems as stated on my edited initial question, mind taking a look? – Luis Afonso Teixeira Jul 17 '12 at 18:20
  • 1
    Override `getColumnClass()` as shown in the original generated code or in the second example cited above. – trashgod Jul 17 '12 at 18:25
  • That fixed the checkbox problem, but I'm still getting and exception when its state changes: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at SimGui$1.actionPerformed(SimGui.java:167) Being 166,167: boolean b = Boolean.parseBoolean(String.valueOf(tcl.getNewValue())); table1model.setColumnEditable(tcl.getColumn(), b); – Luis Afonso Teixeira Jul 17 '12 at 18:33
  • The GUI is still using a custom editor; use the [default](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender). – trashgod Jul 17 '12 at 18:40
  • I am sorry, but could you explain? – Luis Afonso Teixeira Jul 17 '12 at 19:43
  • I believe there is no way to make a cell editable/non-editable but just the whole column?! – Luis Afonso Teixeira Jul 17 '12 at 20:34
  • 1
    No, `isCellEditable()` controls each cell; the default editor is based on column class; please read the [tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) cited, and edit your question to include an [sscce](http://sscce.org/) if you still have problems. – trashgod Jul 18 '12 at 01:07
  • Thank you. I was taking a wrong approach, I read everything and started with the default model and worked my way up and now it works. Fixed code on my question. Thanks a lot! – Luis Afonso Teixeira Jul 18 '12 at 22:13