- 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 disable the cell editing inside a JTable in Java?
A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can fire TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. 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.
Example
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(); } }
Output
- Related Articles
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- 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 filter a JTable in Java?
- How to disable auto resizing for a JTable in Java?
- 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 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?
- How can we disable the leaf of JTree in Java?
- Can we hide the table header from a JTable in Java?
- How can we disable the maximize button of a JFrame in Java?
- How can we detect the double click events of a JTable row in Java?
- How can we display the line numbers inside a JTextArea in Java?

Advertisements