从一个jlist添加到另一个jlist

时间:2015-07-12 16:54:18

标签: java eclipse jlist

我一直在这里进行研究,并一直在寻找解决问题的方法。我是java的新手,所以我不知道所有的语法。我试图让我的代码使用jlists之间的按钮将项目从一个jlist转移到另一个jlist。左侧列表包含要开始的项目。代码假设使用添加按钮将项目从左侧列表移动到右侧列表;还假设使用删除按钮将我们添加到右侧列表中的任何项目移回左侧列表。我的代码将项目从左侧列表添加到右侧列表,但它不会保留添加的项目。我尝试添加的下一个项目只是替换旧项目。有人可以帮帮我吗?以下是整个代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class Window {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Window() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        initialize();
    }

    public void initialize() {
        //Creating the Panel for Menu Bar       
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 23);
        frame.getContentPane().add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        //Creating the Menu File Bar
        JMenuBar bar = new JMenuBar();
        panel.add(bar, BorderLayout.NORTH);
        JMenu file = new JMenu("File");
        JMenuItem load = new JMenuItem("Load");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem exit = new JMenuItem("Exit");
        file.add(load);
        file.add(save);
        file.add(exit);
        bar.add(file);

        //Populate Left List with part names    
        final DefaultListModel parts = new DefaultListModel();
        parts.addElement("Case");
        parts.addElement("Motherboard");
        parts.addElement("CPU");
        parts.addElement("GPU");
        parts.addElement("PSU");
        parts.addElement("RAM");
        parts.addElement("HDD");

        final JList leftList = new JList(parts);
        leftList.setBounds(10, 26, 142, 224);
        frame.getContentPane().add(leftList);

        //create right list 
        final DefaultListModel partSelected = new DefaultListModel();
        final JList rightList = new JList();
        rightList.setBounds(282, 26, 142, 224);
        frame.getContentPane().add(rightList);

        //add event to the button to move items from left list to right list
        JButton btnNewButton = new JButton(">>");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                rightList.setListData(leftList.getSelectedValues());
                for (Object selectedValue : leftList.getSelectedValuesList()) {
                    partSelected.addElement(selectedValue);
                    parts.removeElement(selectedValue);
                    int iSelected = leftList.getSelectedIndex();
                    if (iSelected == -1) {
                        return;
                    }
                }
            }
        });
        btnNewButton.setBounds(172, 86, 89, 23);
        frame.getContentPane().add(btnNewButton);

        //Remove Button     
        JButton remove = new JButton("<<");
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                rightList.setListData(leftList.getSelectedValues());
                for (Object selectedValue : rightList.getSelectedValuesList()) {
                    parts.addElement(selectedValue);
                    int selected = rightList.getSelectedIndex();
                    if (selected == -1) {
                        return;
                    }
                    partSelected.removeElement(selectedValue);
                }
            }
        });
        remove.setBounds(172, 140, 89, 23);
        frame.getContentPane().add(remove);
    }
}

1 个答案:

答案 0 :(得分:1)

@ Prob1em我已经更新了你的代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Window {

    private final JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Window window = new Window();
            window.frame.setVisible(true);

        });
    }

    public Window() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        initialize();
    }

    public void initialize() {
        //Creating the Panel for Menu Bar       
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 23);
        frame.getContentPane().add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        //Creating the Menu File Bar
        JMenuBar bar = new JMenuBar();
        panel.add(bar, BorderLayout.NORTH);
        JMenu file = new JMenu("File");
        JMenuItem load = new JMenuItem("Load");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem exit = new JMenuItem("Exit");
        file.add(load);
        file.add(save);
        file.add(exit);
        bar.add(file);

        //Populate Left List with part names    
        final DefaultListModel parts = new DefaultListModel();
        parts.addElement("Case");
        parts.addElement("Motherboard");
        parts.addElement("CPU");
        parts.addElement("GPU");
        parts.addElement("PSU");
        parts.addElement("RAM");
        parts.addElement("HDD");

        final JList leftList = new JList(parts);
        leftList.setBounds(10, 26, 142, 224);
        frame.getContentPane().add(leftList);

        //create right list 
        final DefaultListModel partSelected = new DefaultListModel();
        final JList rightList = new JList(partSelected);
        rightList.setBounds(282, 26, 142, 224);
        frame.getContentPane().add(rightList);

        //add event to the button to move items from left list to right list
        JButton btnNewButton = new JButton(">>");
        btnNewButton.addActionListener((ActionEvent arg0) -> {
//            rightList.setListData((Vector) leftList.getSelectedValue());
            for (Object selectedValue : leftList.getSelectedValuesList()) {
                partSelected.addElement(selectedValue);
                parts.removeElement(selectedValue);
                int iSelected = leftList.getSelectedIndex();
                if (iSelected == -1) {
                    return;
                }
            }
        });
        btnNewButton.setBounds(172, 86, 89, 23);
        frame.getContentPane().add(btnNewButton);

        //Remove Button     
        JButton remove = new JButton("<<");
        remove.addActionListener((ActionEvent arg0) -> {
//            rightList.setListData(leftList.getSelectedValues());
            for (Object selectedValue : rightList.getSelectedValuesList()) {
                parts.addElement(selectedValue);
                partSelected.removeElement(selectedValue);
                int selected = rightList.getSelectedIndex();
                if (selected == -1) {
                    return;
                }
            }
        });
        remove.setBounds(172, 140, 89, 23);
        frame.getContentPane().add(remove);
    }
}

btw为什么你写了rightList.setListData(leftList.getSelectedValues())并且它也是Java 8中不推荐使用的方法,请在声明变量时遵循正确的命名约定,虽然我没有在这段代码中更改它们,但事件处理函数依照 Java 8 lambda表达式
希望这有帮助