Moving selected items between 2 JList with add/remove buttons using AbstractListModel

北慕城南 提交于 2019-12-25 01:18:44

问题


I'm brand new to programming. It seems the more I research the more I confuse myself, I have to be over thinking what needs to be done. abstract list model is what is needed to make the Jlist work properly. I need to know what is right/wrong. where do I put getselectedvalues(), if it even needs to be used?

JList Left_list = new JList();
    Left_list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
        }
    });
    Left_list.setFont(new Font("Tahoma", Font.PLAIN, 14));
    Left_list.setModel(new AbstractListModel() {
        String[] values = new String[] {"Case", "Motherboard", "CPU", "RAM", "GPU", "HDD", "PSU"};
        public int getSize() {
            return values.length;
        }
        public Object getElementAt(int index) {
            return values[index];
        }
    });
    Left_list.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
    Left_list.setBounds(10, 11, 146, 218);
    frame.getContentPane().add(Left_list);

    JList Right_list = new JList();
    Right_list.setModel(new AbstractListModel() {
        String[] values = new String[] {};
        public int getSize() {
            return values.length;
        }
        public Object getElementAt(int index) {
            return values[index];
        }
    });
    Right_list.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
    Right_list.setBounds(278, 16, 146, 213);
    frame.getContentPane().add(Right_list);

    JButton btnNewButton = new JButton("Add>>");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnNewButton.setBounds(166, 91, 102, 23);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("<<Remove");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnNewButton_1.setBounds(166, 125, 102, 23);
    frame.getContentPane().add(btnNewButton_1);

回答1:


What I would recommend, is using a DefaultListModel instead, as it provides the ability to mutate the ListModel

DefaultListModel<String> model = new DefaultListModel<>();
for (String item : new String[] {"Case", "Motherboard", "CPU", "RAM", "GPU", "HDD", "PSU"}) {
    model.addElement(item);
}
JList<String> Left_list= new JList<>();
Left_list.setModel(model);
//...

JList<String> Right_list = new JList<>(new DefaultListModel<String>());
//...

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        List<String> selected = Left_list.getSelectedValuesList();
        DefaultListModel<String> left = Left_list.getModel();
        DefaultListModel<String> right = Right_list.getModel();
        for (String item : selected) {
            left.removeElement(item);
            right.addElement(item);
        }
    }
});
//...
JButton btnNewButton_1 = new JButton("<<Remove");
btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        List<String> selected = Right_list.getSelectedValuesList();
        DefaultListModel<String> left = Left_list.getModel();
        DefaultListModel<String> right = Right_list.getModel();
        for (String item : selected) {
            right.removeElement(item);
            left.addElement(item);
        }
    }
});

You may need to make Left_list and Right_list instance fields in order for the ActionListeners to access them.

While writing the example, I thought it would be easy to write a simple method which could move data from one list to another, something like protected void move(List<String> items, DefaultListModel from, DefaultListModel to), which means you could simply use move(Left_list.getSelectedValuesList(), Left_list.getModel(), Right_list.getModel()) or move(Right_list.getSelectedValuesList(), Right_list.getModel(), Left_list.getModel()) to move content about and reduce the code duplication...

JLists are really suppose to be displayed in JScrollPanes, this allows the list to be much longer/wider than the available space on the screen

Take a look at

  • How to Use Lists
  • How to Use Scroll Panes
  • Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
  • Laying Out Components Within a Container
  • Creating a GUI With JFC/Swing

...for more details



来源:https://stackoverflow.com/questions/28554376/moving-selected-items-between-2-jlist-with-add-remove-buttons-using-abstractlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!