Siddharth Raina Siddharth Raina - 16 days ago 14
Java Question

How to make a JTable non-editable

How to make a

JTable
non-editable? I don't want my users to be able to edit the values in cells by double-clicking them.

Any help would be greatly appreciated.

Thanks.

Answer

You can use a TableModel.

Define a class like this:

public class MyModel extends AbstractTableModel{
    //not necessary
}

actually isCellEditable() is false by default so you may ommit it. (see: http://docs.oracle.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html)

Then use setModel() method of your JTable.

JTable myTable = new JTable();
myTable.setModel(new MyModel());
Comments