Moving object of a class between two JLists with selectedIndex

32 Views Asked by At

Trying to move object's of a class from one JList too another JList with SelectedIndex.

Have tried reading through Java tutorial but I can't seem to find what I'm looking for.

At the moment I have tried sending in the customerList JList and get the value/index that is selected and just straight up adding it to the desired JList but only generating error with the array and telling me that the index is -1.

I tried doing this instead

public void moveToQ1() {

    index = customerList.getSelectedIndex();
    if (index != -1) {

        browsing.dequeue(customerList);
        q1.enqueue(browsing, customerList, queue1, index);

    }
}

To call the function that moves from browsing to queue1

public void enqueue(QueLine queline, JList list1, JList list2, int index) {

    DefaultListModel<Customer> browseModel = (DefaultListModel<Customer>) list1.getModel();
    DefaultListModel<Customer> queueModel = (DefaultListModel<Customer>) list2.getModel();

    int selectedIndex = index;
    System.out.printf("%d\n", selectedIndex);

    if(selectedIndex >= -1) {
        queueModel.addElement(browseModel.getElementAt(selectedIndex));
    }


}

If I have 3 objects in browseModel and select the second object and press the button to move. The second object will be removed from browseModel but will not be added to queueModel, but instead the third object from browseModel will be added to queueModel.

Also if I try to move the last added object to browseModel to the queueModel I get an "ArrayIndexOutofBoundsException"

Would greatly appreciate if I can be pointed in the direction to figure out of it works.

Edit: switched places on the call for enqueue and dequeue in the first code example and the problem is gone.

0

There are 0 best solutions below