- 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 to select the first column in a JTable with Java?
To select the first column in a JTable, use the setColumnSelectionInterval() method. Here, set the indexes as interval for one end as well as other end.
For the first column, set the range as 0 and 0 since we want to only select the first column with index 0 −
table.setColumnSelectionInterval(0, 0);
The following is an example to select the first column 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(true); table.setRowSelectionAllowed(false); table.setColumnSelectionInterval(0, 0); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
The output is as follows. Now the first column is selected by default −
- Related Articles
- Java Program to select a column in JTable?
- How to select more than one row at a time in a JTable with Java?
- How to select different cells of a JTable programmatically in Java?
- How to change each column width of a JTable in Java?
- Java Program to enable column selection in a JTable
- How to enable row selection in a JTable with Java
- How can we sort a JTable on a particular column in Java?
- Disallow resizing a column by dragging between headers in a JTable Component with Java
- How to display horizontal grid lines in a JTable with Java?
- How to set a value in a particular JTable cell with Java?
- Move the first row to the end of the JTable in Java Swing
- How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?
- How to select all the characters after the first 20 characters from a column in MySQL?
- How to select a column name with spaces in MySQL?
- How to add a new row to JTable with insertRow() in Java Swing

Advertisements