
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add JCheckBox Inside JTable Cell in Java
In this article, we will learn to add/insert a JCheckBox inside a JTable cell in Java. JTable is a powerful component for displaying and editing data in tables. A frequent requirement is to add checkboxes to table cells for boolean data or selection.
JTable
A JTable is a subclass of the JComponent class, and it can be used to create a table with information displayed in multiple rows and columns.
Syntax
The following is the syntax for JTable initialization:
JTable table = new JTable();
When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.
Adding a JCheckBox in a JTable Cell
We can add or insert a checkbox inside a JTable cell by implementing the getColumnClass() method of a Class type.
Step-by-step process for adding/inserting a JCheckBox inside a JTable cell in Java:
Class Definition & Import
The javax.swing.* provides Swing components (JTable, JScrollPane, etc.) while the java.awt.* provides AWT classes (Color, layout managers), and the class extends JFrame to create a window.
import java.awt.*; import java.util.*;
import javax.swing.*; import javax.swing.table.*; public class JCheckBoxJTableTest extends JFrame { }
Instance Variables
It declares two instance variables: the table variable for the JTable component that will display the data, and the model variable for the DefaultTableModel, which will hold the table's data.
private JTable table;
private DefaultTableModel model;
Constructor Setup
This is where all the initializations occur when an instance of the class is being created. Initializes a Random object used to generate random Boolean values for checkboxes.
public JCheckBoxJTableTest() { Random rnd = new Random();
Table Model Setup
Creates a DefaultTableModel with column names: "Check Box1", "Check Box2", and "Check Box3". Overrides getColumnClass() to return Boolean.class for all columns.
model = new DefaultTableModel(new Object[]{"Check Box1","Check Box2", "Check Box3"}, 0) { @Override public Class getColumnClass(int columnIndex) { return Boolean.class; } };
Adding Data to the Model
Adds 10 rows to the model, each row contains a single random boolean value using the rnd.nextBoolean() method. Creates a JTable using the model we configured.
for (int index = 0; index < 10; index++) { model.addRow(new Object[]{rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean()}); } table = new JTable(model);
Main Method
The main method launches the application by creating an object of JCheckBoxJTableTest.
public static void main(String[] args) { new JCheckBoxJTableTest(); }
Example
Below is an example of adding/inserting a JCheckBox inside a JTable cell in Java:
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) { @Override public Class getColumnClass(int columnIndex) { return Boolean.class; } }; for (int index = 0; index < 10; index++) { model.addRow(new Object[]{ rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean() }); } table = new JTable(model); add(new JScrollPane(table)); setTitle("JCheckBoxJTable Test"); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JCheckBoxJTableTest(); } }