1

I'm trying to remove the selection at my JList, after an item has been clicked, because it then can not be clicked again without moving the selection.

I'vet tried this, but keep getting an error, and i don't see what is going on.

    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);

And the error:

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

In your selection handler you are not making any out of bounds check for the selected item

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

What's happening is

  1. The selection changes and enters you handlers code, you process the change and clear the selection
  2. Your handler is notified of the change to the selection (again), you process the change and hit an ArrayOutOfBoundsException because nothing's selected
于 2012-10-27T19:43:18.970 に答える
0

Since relevant code / line #s are missing from your question, my best guess is as follows:

I had a similar problem with selection manipulation for JList. When you are determining the index of what is being selected, the reported index from getMinSelectionIndex() and related calls will sometimes report 0 as negative numbers (may have something to do with deltas, but I am admitted unsure). Place a manual check to ensure that the index you are working with isn't negative:

 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 に答える