- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we add/insert a JCheckBox inside a JTable cell in Java?
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.
Example
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()}); } 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(); } }
Output
- Related Articles
- How can we add/insert a JButton to JTable cell in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- How can we disable the cell editing inside a JTable in Java?
- How can we filter a JTable in Java?
- How can we set a border to JCheckBox in Java?\n
- How can we remove a selected row from a JTable in Java?
- How can we sort a JTable on a particular column in Java?
- How can we add an underscore before each capital letter inside a java String?
- How to create a Default Cell Editor that uses a JCheckBox in Java?
- How to set a value in a particular JTable cell with Java?
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How can we show/hide the table header of a JTable in Java?
- How can we prevent the re-ordering columns of a JTable in Java?
- Can we hide the table header from a JTable in Java?
- Java Program to enable cell selection in a JTable

Advertisements