How to make a JTable non-editable

JavaSwingJtable

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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);

Note for if your JTable disappears

If your JTable is disappearing when you use this it is most likely because you need to use the DefaultTableModel(Object[][] data, Object[] columnNames) constructor instead.

//instance table model
DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) {
    
    @Override
    public boolean isCellEditable(int row, int column) {
       //all cells false
       return false;
    }
};

table.setModel(tableModel);

Solution 2 - Java

table.setDefaultEditor(Object.class, null);

Solution 3 - Java

just add

table.setEnabled(false);

it works fine for me.

Solution 4 - Java

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 omit it. (see: http://docs.oracle.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html)

Then use the setModel() method of your JTable.

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

Solution 5 - Java

If you are creating the TableModel automatically from a set of values (with "new JTable(Vector, Vector)"), perhaps it is easier to remove editors from columns:

JTable table = new JTable(my_rows, my_header);

for (int c = 0; c < table.getColumnCount(); c++)
{
    Class<?> col_class = table.getColumnClass(c);
    table.setDefaultEditor(col_class, null);        // remove editor
}

Without editors, data will be not editable.

Solution 6 - Java

I used this and it worked : it is very simple and works fine.

JTable myTable = new JTable();
myTable.setEnabled(false);

Solution 7 - Java

create new DefaultCellEditor class :

public static class Editor_name extends DefaultCellEditor {
  public Editor_name(JCheckBox checkBox) {
   super(checkBox);
  }
  @Override
  public boolean isCellEditable(EventObject anEvent) {
    return false;
  }
}

and use setCellEditor :

JTable table = new JTable();
table.getColumn("columnName").setCellEditor(new Editor_name(new JCheckBox()));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiddharth RainaView Question on Stackoverflow
Solution 1 - Javanelson eldoroView Answer on Stackoverflow
Solution 2 - JavaOleg MikhailovView Answer on Stackoverflow
Solution 3 - JavaSiddhuView Answer on Stackoverflow
Solution 4 - JavaJCassoView Answer on Stackoverflow
Solution 5 - JavafreesoftView Answer on Stackoverflow
Solution 6 - Javauser3518835View Answer on Stackoverflow
Solution 7 - JavaEhsan JelodarView Answer on Stackoverflow