1

I have an array list with several objects in it, and these are shown in a JList panel. I want to select an object and when I press a button it will add the selected item to another ArrayList. That one will also be shown on a second JList.

The code below shows the work I have done so far:

import java.util.ArrayList;

 /**
 * 
 * ArrayList for the class, will hold all food items
 * @author Jonathan
 * @version 1.0
 *
 */
public class RestaurantArrayList extends MenuItem
{  
    public RestaurantArrayList(String nameFood, String typeFood, float foodPrice, int caloryCount) {
        super(nameFood, typeFood, foodPrice, caloryCount);
    }

    public static final ArrayList<MenuItem> items;

    static {
        items = new ArrayList<>();
        items.add(new MenuItem("Coca Cola", "Drink", 3.00f, 38));
        items.add(new MenuItem("Fanta Orange", "Drink", 3.00f, 31 ));
        items.add(new MenuItem("Glass of Red Wine", "Drink", 5.00f, 85));
        items.add(new MenuItem("Glass of White Wine", "Drink", 5.00f, 82));
        items.add(new MenuItem("Carling", "Drink", 3.50f, 189));
        items.add(new MenuItem("Fosters", "Drink", 3.50f, 378));
        items.add(new MenuItem("Water", "Drink", 0.00f, 0));
        items.add(new MenuItem("Breads", "Starter", 5.00f, 150));
        items.add(new MenuItem("Cold Meat", "Starter", 5.00f, 150));
        items.add(new MenuItem("Potato Skins and Barbeque Sauce", "Starter", 5.00f, 500));
        items.add(new MenuItem("Cold Meat", "Starter", 5.00f, 400));
        items.add(new MenuItem("Garlic Bread and Cheese", "Starter", 4.50f, 450));
        items.add(new MenuItem("Steak", "Main", 13.50f, 750));
        items.add(new MenuItem("Cheese and Bacon Burger", "Main", 8.00f, 850));
        items.add(new MenuItem("Spaghetti Cabonara", "Main", 7.00f, 675));
        items.add(new MenuItem("Steak", "Main", 13.50f, 378));
        items.add(new MenuItem("Seafood Paella", "Main", 10.00f, 850));
    }
}

Here is the first ArrayList with all my items added into the Array.

JButton button = new JButton(">>");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        button.setBounds(333, 180, 59, 25);
        contentPane.add(button);

Here is the button I need to work with an action listener. But I'm not sure what to put into the action listener. I also haven't got the second array yet, because I don't know how to set it up, so I can dynamically add objects into it.

If I am going about this in a weird way then I'm open to suggestions, remember I'm new so I might be going about it in a long winded method.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • 1
    Your code is not complete and clear enough. Where is your another `Arraylist` defined in your codes? Please provide a http://stackoverflow.com/help/mcve – STaefi Apr 12 '16 at 10:46
  • I'm not sure what you mean, are you talking about the ArrayList I have shown or the one I said I haven't made yet? – Jonathan Gedlek Apr 12 '16 at 10:54
  • please append code related to populate first and second JList (are you using `DefaultListModel`) ? – Yazan Apr 12 '16 at 11:29
  • 1
    Something like [this](http://stackoverflow.com/questions/28554376/moving-selected-items-between-2-jlist-with-add-remove-buttons-using-abstractlist/28554424#28554424)? – MadProgrammer Apr 12 '16 at 11:30
  • @STaefi Great suggestion. A tip: `[mcve]` (without the code formatting) in a comment will auto-expand to [mcve]. – Andrew Thompson Apr 12 '16 at 12:08
  • 1
    `button.setBounds(333, 180, 59, 25);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 12 '16 at 12:08
  • 1
    @AndrewThompson: Many thanks for the tip, always learning from you. I was looking for how to embed it! Many thanks again! – STaefi Apr 12 '16 at 12:28

2 Answers2

0

Is this what you're looking for?

    final JList<MenuItem> firstJList = new JList<MenuItem>();
    DefaultListModel<MenuItem> firstModel = new DefaultListModel<MenuItem>();
    for (MenuItem item : RestaurantArrayList.items) {
        firstModel.addElement(item);
    }
    firstJList.setModel(firstModel);

    final JList<MenuItem> secondJList = new JList<MenuItem>();
    secondJList.setModel(new DefaultListModel<MenuItem>());

    JButton button = new JButton(">>");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (firstJList.isSelectionEmpty())
                return;
            List<MenuItem> selection = firstJList.getSelectedValuesList();
            DefaultListModel<MenuItem> model = (DefaultListModel<MenuItem>) secondJList.getModel();
            for (MenuItem selected : selection) {
                if (!model.contains(selected))
                    model.addElement(selected);
            }
        }
    });

So basically what the above code does is that it gets the selection list form the first list firstJList and adds it to the secondJList when the button action is performed.

can lekili
  • 255
  • 1
  • 8
0

I have an array list with several objects in it, and these are shown in a JList panel. I want to select an object and when I press a button it will add the selected item to another ArrayList. That one will also be shown on a second JList.

No. Your approach is all wrong. You should NOT be dealing with an ArrayList. Swing uses a Model-View-Controller design.

Basically this means that all the data is displayed in the Model. The View will then display the data in the Model. In your case the JList will display the data stored in the ListModel.

So the basic code for creating the JList would be:

listModel = new DefaultListModel();
listModel.addElement( new MenuItem("Coca Cola", "Drink", 3.00f, 38) );
...
list = new JList(listModel);

There is no need for the ArrayList. Then whenever you want to change the data you update the model.

So start by downloading and playing with the ListDemo example found in the Swing tutorial How to Use Lists.

The demo shows you how to dynamically add an item to a model when you press a button and how to remove an item from a model when you click a button. So apply the concepts of both buttons into a single button and you have the logic to move an item from one JList to another.

remember I'm new so I might be going about it in a long winded method.

Keep a link to the Swing tutorial handy for learning about all the Swing basics. You were also given a link to this tutorial in your last question. We should not have to keep repeating ourselves. The basics are there it is up to you to do the reading.

camickr
  • 321,443
  • 19
  • 166
  • 288