Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically select a row in JTable

When the application is started, none of the rows is selected. But I would like to show that the first row is already selected.

How to do this? Do I need to set the color of a row in JTable?

Update: I tried table.setRowSelectionInterval(0,0). I know it should work, but there is an error message:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Row index out of range     at javax.swing.JTable.boundRow(Unknown Source)     at javax.swing.JTable.setRowSelectionInterval(Unknown Source)     at cpn_gui.OptPanel.createForm(OptPanel.java:124)     at cpn_gui.OptPanel.<init>(OptPanel.java:50)     at cpn_gui.Login$1.actionPerformed(Login.java:62)     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)     at java.awt.Component.processMouseEvent(Unknown Source)     at javax.swing.JComponent.processMouseEvent(Unknown Source)     at java.awt.Component.processEvent(Unknown Source)     at java.awt.Container.processEvent(Unknown Source)     at java.awt.Component.dispatchEventImpl(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Window.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)     at java.awt.EventQueue.access$000(Unknown Source)     at java.awt.EventQueue$1.run(Unknown Source)     at java.awt.EventQueue$1.run(Unknown Source)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)     at java.awt.EventQueue$2.run(Unknown Source)     at java.awt.EventQueue$2.run(Unknown Source)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)     at java.awt.EventQueue.dispatchEvent(Unknown Source)     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     at java.awt.EventDispatchThread.run(Unknown Source) 
like image 719
Klausos Klausos Avatar asked Dec 28 '11 21:12

Klausos Klausos


People also ask

How to select a row in JTable programmatically?

You can do it calling setRowSelectionInterval : table. setRowSelectionInterval(0, 0); to select the first row.

How to select a cell in JTable?

The JTable control supports various keyboard shortcuts which can be used to navigate through the grid. For example, pressing an arrow key selects the adjacent cell in the direction of the arrow, Ctrl+Home navigates to the upper cell in the selected column, Ctrl+End navigates to the bottom cell, and so on.

How do you delete a selected row in Java?

We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.


2 Answers

You can do it calling setRowSelectionInterval :

table.setRowSelectionInterval(0, 0); 

to select the first row.

like image 178
aleroot Avatar answered Sep 19 '22 08:09

aleroot


It is an old post, but I came across this recently

Selecting a specific interval

As @aleroot already mentioned, by using

table.setRowSelectionInterval(index0, index1); 

You can specify an interval, which should be selected.

Adding an interval to the existing selection

You can also keep the current selection, and simply add additional rows by using this here

table.getSelectionModel().addSelectionInterval(index0, index1); 

This line of code additionally selects the specified interval. It doesn't matter if that interval already is selected, of parts of it are selected.

like image 40
rst Avatar answered Sep 17 '22 08:09

rst