ghost1349
ghost1349

Reputation: 83

Using JLists In Java Swing

I have to work with JLists for a project and I am stuck trying to do a couple of things. Here are my lists:

JList<String> BooksList = new JList<String>(booksList);
books.add(BooksList, BorderLayout.CENTER);
BooksList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);


JList cartList = new JList();
cart.add(cartList, BorderLayout.CENTER);
cartList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

In The BooksList are the following items:

I Did It Your Way;11.95
The History of Scotland;14.50
Learn Calculus in One Day;29.95
Feel the Stress;18.50
Great Poems;12.95
Europe on a Shoestring;10.95
The Life of Mozart;14.50

1.) Moving Items From BooksList to cartList, specifically I need it to append the newly added items but if I try to add items one at a time then it will replace what is already in cartList with the new item. here is the code I have:

//Adding To Cart
JButton AddToCart = new JButton("Add To Cart");
AddToCart.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> selectionList = (ArrayList<String>) BooksList.getSelectedValuesList();
        Object[] selections = selectionList.toArray();
        cartList.setListData(selections);
    }
});
AddToCart.setToolTipText("Alt + A For Auto Add");
AddToCart.setBounds(264, 178, 117, 25);
AddToCart.setMnemonic(KeyEvent.VK_A);
frame.getContentPane().add(AddToCart);

2.) Clearing The Cart List Completely, for some reason nothing happens when this clicked. here is the code:

//This Will Clear The Whole Cart List
JMenuItem Clear = new JMenuItem("Clear                                  Alt + C");
cartMenu.add(Clear);
Clear.setMnemonic(KeyEvent.VK_C);
Clear.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        DefaultListModel tempModel = (DefaultListModel)cartList.getModel();
        tempModel.removeAllElements();  
    }

});

3.) Removing Selected Items, same thing as 2 it just doesn't do anything. I have the following code:

//Remove A Selected Item From The List
JMenuItem RemoveSelected = new JMenuItem("Remove Selected             Alt + R");
cartMenu.add(RemoveSelected);
RemoveSelected.setMnemonic(KeyEvent.VK_R);
RemoveSelected.addActionListener(new ActionListener (){
    public void actionPerformed(ActionEvent e) {
        DefaultListModel tempModel = (DefaultListModel)cartList.getModel();
        int selected = cartList.getSelectedIndex();
        if(selected != -1)
        {
            tempModel.remove(selected);
        }
    }
});

Upvotes: 0

Views: 302

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

When you add to the JList, you want to add it to its ListModel rather than directly:

DefaultListModel tempModel = (DefaultListModel) cartList.getModel();
for (String s: BooksList.getSelectedValuesList())
    tempModel.addElement(s);

I haven't had a chance to test that, but that's the right approach. Currently, you're calling .setListData(), which clears out what's there and replaces it. This will add something to it instead.

You might find this question helpful.

Upvotes: 4

Related Questions