以编程方式在 JTable 中选择一行

2022-08-31 17:08:46

启动应用程序时,不会选择任何行。但我想表明第一行已经被选中。

如何做到这一点?我需要设置行的颜色吗?JTable

更新:我尝试了 table.setRowSelectionInterval(0,0)。我知道它应该工作,但有一条错误消息:

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)

答案 1

你可以调用 setRowSelectionInterval

table.setRowSelectionInterval(0, 0);

以选择第一行。


答案 2

这是一篇旧帖子,但我最近遇到了这个问题

选择特定间隔

如前所述@aleroot,通过使用

table.setRowSelectionInterval(index0, index1);

您可以指定一个间隔,应选择该间隔。

向现有所选内容添加间隔

您还可以保留当前选择,只需在此处使用此选项添加其他行即可

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

此行代码还选择指定的间隔。是否已经选择了该间隔并不重要,选择了其中的一部分。