我正在尝试在单击某个项目后删除我的JList中的选择,因为如果不移动选择则无法再次单击它。
我试过这个,但一直收到错误,我看不出发生了什么。
ListSelectionModel mode = new DefaultListSelectionModel();
mode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mode.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent evt)
{
// Åben url'en i en browser
if( !evt.getValueIsAdjusting() )
{
if(isDownloaded(data_alle.get(list_alle.getSelectedIndex()),data_downloaded))
{
try {
Desktop.getDesktop().open(new File(data_downloaded.get(list_alle.getSelectedIndex()).getPath()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
OpenURI.open(data_alle.get(list_alle.getSelectedIndex()).getUrl(), url);
}
list_alle.clearSelection();
}
}
});
list_alle.setSelectionModel(mode);
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at gui.Kursus$2.valueChanged(Kursus.java:114)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(Unknown Source)
at javax.swing.DefaultListSelectionModel.clearSelection(Unknown Source)
at javax.swing.JList.clearSelection(Unknown Source)
at gui.Kursus$2.valueChanged(Kursus.java:127)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(Unknown Source)
at javax.swing.JList.setValueIsAdjusting(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.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)
答案 0 :(得分:2)
在您的选择处理程序中,您没有对所选项目进行任何越界检查
if( !evt.getValueIsAdjusting() && list_alle.getSelectedIndex() >= 0) {...}
发生了什么
ArrayOutOfBoundsException
因为没有选择答案 1 :(得分:0)
由于你的问题缺少相关的代码/行#,我的最佳猜测如下:
我在JList的选择操作方面遇到了类似的问题。当您确定所选内容的索引时,来自getMinSelectionIndex()
的报告索引和相关呼叫有时会将0报告为负数(可能与增量有关,但我承认不确定)。进行手动检查以确保您使用的索引不是否定的:
89 class SharedListSelectionHandler implements ListSelectionListener {
90 public void valueChanged(ListSelectionEvent e) {
91 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
92
93 if (!lsm.getValueIsAdjusting()){
94 int index = lsm.getMinSelectionIndex();
95 if (index < 0) {
96 index = 0;
97 }
98 handleListSelectionEvent(index);
99 }
100
101 }
102 }