- 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
How to select more than one row at a time in a JTable with Java?
To select more than one row in a JTable, use the setRowSelectionInterval() method. Here, set the indexes as interval for one end as well as other end.
For multiple rows in a range, set the range. Here, we are selecting rows from index 1 to index 2 i.e. two rows −
table.setRowSelectionInterval(1, 2);
The following is an example to select more than one row at a time 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.ListSelectionModel; 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.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(true); table.addRowSelectionInterval(1, 2); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- How to sort more than one column at a time in MySQL?
- How to enable row selection in a JTable with Java
- How to select one item at a time from JCheckBox in Java?
- How to select the first column in a JTable with Java?
- How to add a new row to JTable with insertRow() in Java Swing
- Java Program to select a column in JTable?
- Java Program to append a row to a JTable in Java Swing
- How to select different cells of a JTable programmatically in Java?
- Suggest a situation where we obtain more than one shadow of an object at a time.
- MySQL query to select rows one batch at a time
- Use JOIN to select record with more than one condition using AND?
- Java Program to increase the row height in a JTable
- How can we remove a selected row from a JTable in Java?
- Create a Pandas Dataframe by appending one row at a time
- Insert more than one element at once in a C# List

Advertisements