- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java Program to enable cell selection in a JTable
To enable cell selection, use the setCellSelectionEnabled() method and set it to TRUE −
table.setCellSelectionEnabled(true);
The above sets whether this table allows both a column selection and arow selection to exist simultaneously.
The following is an example to enable cell selection in a JTable −
Example
package my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP)); String[][] rec = { { "1", "Steve", "AUS" }, { "2", "Virat", "IND" }, { "3", "Kane", "NZ" }, { "4", "David", "AUS" }, { "5", "Ben", "ENG" }, { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header); table.setShowHorizontalLines(true); table.setGridColor(Color.orange); table.setCellSelectionEnabled(true); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
The output is as follows. Here, we have selected a single selection. We can select a cell now since setCellSelectionEnabled() is set TRUE above −
- Related Articles
- Java Program to enable column selection in a JTable
- How to enable row selection in a JTable with Java
- Java Program to retrieve the value from a cell in a JTable
- What are the Selection Modes in a JTable with Java?
- How to set a value in a particular JTable cell with Java?
- How can we add/insert a JButton to JTable cell in Java?
- Java Program to select a column in JTable?
- How to validate if JTable has an empty cell in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- Java Program to append a row to a JTable in Java Swing
- Java Program to deselect all cells in a JTable
- How can we disable the cell editing inside a JTable in Java?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How to enable selection of object only when it is fully contained in a selection area in FabricJS?
- Java Program to increase the row height in a JTable

Advertisements