How to make a jtable not editable in java?

 private TableModel model = new DefaultTableModel(data, columnNames)
  {
    public boolean isCellEditable(int row, int column)
    {
      return false;//This causes all cells to be not editable
    }
  };
  private JTable table = new JTable(model);

Edited. If you are doing this in Netbeans IDE designer, follow the steps below:

  • Select the form on which the JTable is placed
  • From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:

Navigator Pane

  • On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
  • Now paste this: {public boolean isCellEditable(int row, int column){return false;}} before the last closing blacket );

Your final setup should look as shown below:

  • Press ok to save - and job done.

Code Customizer


If you use DefaultTableModel you can override the method isCellEditable and implement it when constructing GUI:

table.setModel(new DefaultTableModel() {

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