0

i created a GUI with an ADD and REMOVE button to save items to a JList. The add button works perfectly but when i want to remove something i stumble across a problem. My remove button works like this, when i press it, i get the selected index that i have marked and removes the item at the index. My problem is that when i press the item i want to remove and then hit REMOVE it doesnt save the index and it just returns -1 which tells me that no index has been marked. Any tips how i work around this?

 private void removeProductActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    productList.getModel();
    int selectedIndex = productList.getSelectedIndex();

    if(selectedIndex != -1){

        listModel.remove(selectedIndex);
    }
} 

2 Answers 2

0

are you sure your JList is setup correctly?

public static void main(String[] args) {

    DefaultListModel<String> model = new DefaultListModel<>();
    model.addElement("1");
    model.addElement("2");
    model.addElement("3");
    model.addElement("4");

    JList<String> test = new JList<>(model);
    test.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    test.setSelectedIndex(2);

    System.out.println("items before removal");
    for (int i = 0; i < model.getSize(); i++) {
        System.out.println(model.getElementAt(i));
    }

    int selectedIndex = test.getSelectedIndex();
    if (selectedIndex > 0) {
        System.out.println(String.format("removing item at index %d : %s", selectedIndex, model.getElementAt(selectedIndex)));
        model.remove(selectedIndex);
    }

    System.out.println("items after removal");
    for (int i = 0; i < model.getSize(); i++) {
        System.out.println(model.getElementAt(i));
    }
}
2
  • I'm pretty sure it is correctly. If i by example just remove the first item on index 0 "listModel.remove(0);" when pressing the button it works perfectly. So i'm sure that the index isnt saved when hitting the REMOVE button and i cant figure out a way to go around this Apr 29, 2016 at 16:14
  • try attaching a list selection listener and add some debug logging to it (print selected indexes etc.) check that the selection mode is set correctly.
    – kalvatn
    Apr 29, 2016 at 16:35
0

So i did find a solution, i dont know if it was the best but it is working. If anyone finds anything which will be better just post it.

What i've done is i'm using a listselectionlistener and when a item is pressed its saves the index into a global variable which my REMOVE button reads the value from.

    private void indexListener() {
    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            productList.getModel();
            selectedIndex = listSelectionEvent.getFirstIndex();


        }

    };
    productList.addListSelectionListener(listSelectionListener);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.