How can we add/insert a JCheckBox inside a JTable cell in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface. We can add or insert a checkbox inside a JTable cell by implementing the getColumnClass() method of a Class type.


import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JCheckBoxJTableTest extends JFrame {
   private JTable table;
   private DefaultTableModel model;
   public JCheckBoxJTableTest() {
      Random rnd = new Random();
      model = new DefaultTableModel(new Object[]{"Check Box1","Check Box2", "Check Box3"}, 0) {
         public Class getColumnClass(int columnIndex) {
            return Boolean.class;
      for (int index = 0; index < 10; index++) {
         model.addRow(new Object[]{rnd.nextBoolean()});
      table = new JTable(model);
      add(new JScrollPane(table));
      setTitle("JCheckBoxJTable Test");
      setSize(375, 250);
   public static void main(String[] args) {
      new JCheckBoxJTableTest();


Published on 18-Jul-2019 18:29:38