tags:

views:

887

answers:

2

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.

+1  A: 

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());
JCasso
You can't have a `public void` method return a boolean.
Geo
Also the method is `isCellEditable`
Matt
While the approach you specify works, there is no such method as isEditable in the AbstractTableModel. What exists is the method isCellEditable(int,int) which takes rowIndex and coulmnIndex as parameters. The user can selectively enable/disable editing for a particular row/column by overriding "isCellEditable" method or can use the default implementation to disable editing for all cells.
sateesh
As i pointed it is not needed, but thank you for fixing.
JCasso
+2  A: 

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);
nelson eldoro
This is the most simple solution indeed for someone who just wants a default table model, only not editable.
Gnoupi