57

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)
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

3 Answers3

111

You can do it calling setRowSelectionInterval :

table.setRowSelectionInterval(0, 0);

to select the first row.

cxw
  • 16,685
  • 2
  • 45
  • 81
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • Please check the updates in my post. I posted an error message. What could be wrong? – Klausos Klausos Dec 28 '11 at 21:26
  • My code looks as follows: QueryTableModel tableModel = new QueryTableModel(); JTable table = new JTable(tableModel); table.setRowSelectionInterval(0, 0); – Klausos Klausos Dec 28 '11 at 21:41
  • 2
    Before calling table.setRowSelectionInterval(0, 0) you have to make sure that at least a row is present in the table, otherwise you will get this error. Call table.setRowSelectionInterval(0, 0) after filling the table ... – aleroot Dec 28 '11 at 21:43
14

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.

rst
  • 2,510
  • 4
  • 21
  • 47
  • 1
    This is more general answer, that covers both consecutive and non-consecutive row selection. It worked for me. – Kamal Singh Apr 01 '16 at 07:00
6

You use the available API of JTable and do not try to mess with the colors.

Some selection methods are available directly on the JTable (like the setRowSelectionInterval). If you want to have access to all selection-related logic, the selection model is the place to start looking

Robin
  • 36,233
  • 5
  • 47
  • 99
  • I tried table.setRowSelectionInterval(1,1), but it says that the "Row index out of range", although the table was already populated from DB. Any ideas? – Klausos Klausos Dec 28 '11 at 21:07
  • 1
    As almost everything in Java, numbering starts from row 0 and not row 1. So change it to `setRowSelectionInterval(0,0)` – Robin Dec 28 '11 at 21:09