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.
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.
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://java.sun.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());
You can override the method isCellEditable and implement as you want for example:
//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
table.setModel(tableModel);
or
//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
//Only the third column
return column == 3;
}
};
table.setModel(tableModel);