简体   繁体   中英

Removing items from JList

I've got a simple Jlist with data from List<String> , Now I want to remove selected item from Jlist. Here is the code:

final DefaultListModel<String> model = new DefaultListModel();
final JList list = new JList(model);

//filling list
//loop for every element from List<String>
 public static void sample(DefaultListModel model, List<String> data)
      for(int i=;i<data.size();i++)
        {model.addElement(data.get(i));}

//btn pressed
public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    model.removeElementAt(index);
}

I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
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.removeIndexInterval(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultListModel.remove(Unknown Source)
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$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)

Is the any trick or something? Thanks for help.

Assuming your index is non-negative (as mentioned by others), see if this works (in your listener):

((DefaultListModel) jList.getModel()).remove(index);

If so, then you're using using a stale model.

According to the javadoc , using remove() instead of removeElementAt() is recommended, so :

public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    if (index != -1) {
        model.remove(index);
}

According to the Javadoc for getSelectedIndex() :

Returns the smallest selected cell index; the selection when only a single item is selected in the list. When multiple items are selected, it is simply the smallest selected index. Returns -1 if there is no selection

The reason that you're experiencing the error is because for some reason, no items are selected from your list and as such -1 is returned by this method. When you call removeElementAt() and pass it -1 as a parameter value, it would throw you the exception.

What you need to do is as follows:

public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    if(index >= 0){ //Remove only if a particular item is selected
        model.removeElementAt(index);
    }
}

The question is that you have a problem in the listener, because when the element is removed the selected value will change. This is the reason that your "valueChanged" method is trying to get the selectedValue in a wrong position. I can't see your method valueChanged, but I think this is the reason.

DefaultListModel model=new DefaultListModel();
    model.clear();
 jList1.setModel(model);

if you want delete all item

int selectedIndex = yourJLIST.getSelectedIndex();
    String [] ListData = new String[yourJLIST.getModel().getSize()];
    for (int i = 0; i < ListData.length; i++) {
        if(i == selectedIndex){
            
        }else{
            ListData[i] = yourJLIST.getModel().getElementAt(i);
        }
    }
    yourJLIST.setListData(ListData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM