Disable user edit in JTable

You can create a JTable using following code:

    JTable jTable = new JTable() {
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int row, int column) {                
                return false;               
        };
    };

Basically what we are doing here is overriding isCellEditable and always returning false from it. This will make a non editabe JTabel.


myTable.setDefaultEditor(Object.class, null);

Have you tryed simply:

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

About JComponent.setEnabled(boolean) it sayes:

Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.

When it comes to JTable it doesnt seem to give any visual feedback at all. With the perk of still being able to click on the column headers. And in my implementation the application could still change the contents of the cells.


A JTable uses an AbstractTableModel object. This is the thing you pass into the constructor of the JTable. You can write your own AbstractTableModel as follows

public class MyTableModel extends AbstractTableModel {

      public boolean isCellEditable(int row, int column){  
          return false;  
      }

}

and then initialize your JTable as

JTable myTable = new JTable(new MyTableModel());