1

在单击一个项目后,我试图在我的 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)
4

2 回答 2

2

在您的选择处理程序中,您没有对所选项目进行任何越界检查

if( !evt.getValueIsAdjusting() && list_alle.getSelectedIndex() >= 0) {...}

正在发生的事情是

  1. 选择更改并输入您的处理程序代码,您处理更改并清除选择
  2. 您的处理程序(再次)收到有关选择更改的通知,您处理更改并点击 an,ArrayOutOfBoundsException因为没有选择任何内容
于 2012-10-27T19:43:18.970 回答
0

由于您的问题中缺少相关代码/行#s,我最好的猜测如下:

我对 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     }
于 2012-10-27T17:17:18.013 回答