Java Swing How to - Make a specific jTable boolean column editable








Question

We would like to know how to make a specific jTable boolean column editable.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/*from   w ww. ja v  a 2s.c o m*/
public class Main {

  public static void main(String... s) {

    DefaultTableModel model = new DefaultTableModel(4, 4) {
      @Override
      public boolean isCellEditable(int row, int column) {
        return column == 3;
      }

      @Override
      public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 3) {
          return Boolean.class;
        }
        return super.getColumnClass(columnIndex);
      }
    };

    JTable t = new JTable(model);
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
  }
}