Disable user edit in JTable

edit, java, jtable, swing

Solution

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

Problem

When a JTable component is created, cell editing is enabled by default. How can I prevent the user from editing the content of a JTable?

Original source

Related problems