
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
How can we disable the cell editing inside a JTable in Java?
When working with JTables in Java Swing, there are many cases where one might need to display data that shouldn't be modified by users. In this article, we will learn to disable the cell editing inside a JTable in Java.
JTable
A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern to display the data in rows and columns.
A JTable can fire TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, and RowSorterListener interfaces.
editCellAt() Method
The editCellAt() method is used to prevent the JTable from editing a particular table, column, or cell value. Return false if, for any reason, the cell cannot be edited. The editCellAt() method takes 3 arguments as parameters: row, column, and the event object.
Method Declaration:
public boolean editCellAt(int row, int column, EventObject e)
Disabling the Cell Editing Inside a JTable
By default, we can edit the text and modify it inside a JTable cell. We can also disable the cell editing inside a table by calling the editCellAt() method of JTable class and it must return false.
The following are the steps for disabling the cell editing inside a JTable in Java:
Table Data Initialization
Declaring a string array named "columnNames" to define 2 columns, Country, and Rank. A 2D Object array, named "data", which contains country rankings, and has 6 rows.
String[] columnNames = {"Country", "Rank"}; Object[][] data = { {"England", "1"}, {"India", "2"}, {"New Zealand", "3"}, {"Australia", "4"}, {"South Africa","5"}, {"Pakistan","6"} };
Custom JTable Implementation
Creates a subclass of JTable to override the editCellAt() method so that it always returns false, and disables all cell editing in the JTable. Setting the setRowSelectionAllowed(false) disables the row selection entirely.
table = new JTable(data, columnNames) { public boolean editCellAt(int row, int column, java.util.EventObject e) { return false; } }; table.setRowSelectionAllowed(false);
ScrollPane Setup
The JScrollPane wraps the table in a "scrollPane" and adds the scrollPane to the JFrame using the add() method.
scrollPane = new JScrollPane(table); add(scrollPane);
Example
Below is an example of disabling the cell editing inside a JTable in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public final class DisableJTableMouseClickTest extends JFrame { private JTable table; private JScrollPane scrollPane; public DisableJTableMouseClickTest() { setTitle("DisableJTableMouseClick Test"); String[] columnNames = {"Country", "Rank"}; Object[][] data = {{"England", "1"}, {"India", "2"}, {"New Zealand", "3"}, {"Australia", "4"}, {"South Africa","5"}, {"Pakistan","6"}}; table = new JTable(data, columnNames) { public boolean editCellAt(int row, int column, java.util.EventObject e) { return false; } }; table.setRowSelectionAllowed(false); scrollPane= new JScrollPane(table); add(scrollPane); setSize(400, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new DisableJTableMouseClickTest(); } }