使用AbstractListModel使用添加/删除按钮在2个JList之间移动所选项目 [英] Moving selected items between 2 JList with add/remove buttons using AbstractListModel
问题描述
我是编程的新手.似乎我研究的越多,我对自己的困惑也就越多,我不得不思考需要做些什么.要使Jlist
正常工作,需要抽象列表模型.我需要知道什么是对/错.如果甚至需要使用getselectedvalues()
,我应该放在哪里?
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);
推荐答案
我建议使用的是DefaultListModel
,因为它提供了对ListModel
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);
}
}
});
您可能需要设置Left_list
和Right_list
实例字段,以便ActionListener
能够访问它们.
You may need to make Left_list
and Right_list
instance fields in order for the ActionListener
s to access them.
在编写示例时,我认为编写一种简单的方法可以轻松地将数据从一个列表移动到另一个列表,例如protected void move(List<String> items, DefaultListModel from, DefaultListModel to)
,这意味着您可以简单地使用move(Left_list.getSelectedValuesList(), Left_list.getModel(), Right_list.getModel())
或move(Right_list.getSelectedValuesList(), Right_list.getModel(), Left_list.getModel())
进行移动内容并减少代码重复...
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...
JList
实际上应该显示在JScrollPane
s中,这使得列表比屏幕上的可用空间长/宽得多
JList
s are really suppose to be displayed in JScrollPane
s, this allows the list to be much longer/wider than the available space on the screen
看看
- 如何使用列表
- 如何使用滚动窗格
- Java TM编程语言的代码约定,它将使人们更容易阅读您的代码,而您也可以阅读其他人
- 在容器内布置组件
- 使用JFC/Swing创建GUI
- 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
...了解更多详情
这篇关于使用AbstractListModel使用添加/删除按钮在2个JList之间移动所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!