移动从一个ArrayList的对象到另一个与Swing [英] Moving objects from one ArrayList to another with Swing

查看:232
本文介绍了移动从一个ArrayList的对象到另一个与Swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与它的几个对象的数组列表,这些都将在JList面板中显示。我想选择一个对象,当我preSS一个按钮,将选定的项目添加到另一个的ArrayList。那一个也将在第二JList中显示。

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.

在code以下显示到目前为止,我所做的工作:

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));
    }
}

这是第一个使用ArrayList中添加到阵列我的所有项目。

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.

推荐答案

这是你要找的是什么?

    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);
            }
        }
    });

所以基本上什么上述code的作用是它得到了选择列表形成一个列表 firstJList ,并将其添加到 secondJList 在执行按钮动作。

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.

这篇关于移动从一个ArrayList的对象到另一个与Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆