
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to select different cells of a JTable programmatically 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.
In general, a user can select the rows and columns manually in a JTable, we can also select different cells of a JTable programmatically using setRowSelectionInterval() and setColumnSelectionInterval() methods of JTable class.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTableCellSelectionTest extends JFrame { private JTable table; public JTableCellSelectionTest() { setTitle("JTableCellSelection Test"); Object[][] data = {{ "Raja", "Java", "Hyderabad"}, {"Vineet", "JavaScript", "Bangalore"}, {"Adithya", "Scala", "Chennai"}, {"Jai", "ServiceNow", "Pune"}, {"Chaitanya", "Python", "Noida"}, {"Krishna", "AI", "Mumbai"}}; String columns[] = {"Name", "Technology", "Location"}; table = new JTable(data, columns); add(new JScrollPane(table)); table.setRowSelectionInterval(0, 2); table.setColumnSelectionInterval(0, 2); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String []args) { new JTableCellSelectionTest(); } }
Output
- Related Articles
- How to set magins between cells of a JTable in Java?
- Java Program to select a column in JTable?
- Java Program to deselect all cells in a JTable
- How to select the first column in a JTable with Java?
- How to select more than one row at a time in a JTable with Java?
- Java Program to select all cells in a table
- Java Program to set different height for multiple rows in JTable
- How to change each column width of a JTable in Java?
- How to implement the search functionality of a JTable in Java?
- How to add a title to JTable in Java Swing?
- How can I programmatically select a specific subplot in Matplotlib?
- How to disable auto resizing for a JTable in Java?
- How to enable row selection in a JTable with Java
- How can we filter a JTable in Java?
- How to programmatically set the value of a select box element using JavaScript?

Advertisements