以编程方式在JTable中select一行

当应用程序启动时,没有任何行被选中。 但是我想表明第一行已经被选中。

这个怎么做? 我需要在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) 

你可以这样做,调用setRowSelectionInterval :

 table.setRowSelectionInterval(0, 0); 

select第一行。

这是一个旧的post,但我最近碰到这个

select特定的时间间隔

正如@aleroot已经提到的那样,通过使用

 table.setRowSelectionInterval(index0, index1); 

您可以指定应该select的时间间隔。

将间隔添加到现有的select

您也可以保留当前的select,并在这里使用这个简单地添加额外的行

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

这行代码还会select指定的时间间隔。 这个时间间隔是否已经被选中并不重要,其中的一部分被选中。

您使用JTable的可用API ,不要试图混淆颜色。

有些select方法可直接在JTable (如setRowSelectionInterval )。 如果你想访问所有与select有关的逻辑, select模型是开始寻找的地方

Interesting Posts