我是编程的新手。似乎我研究的越多,我就越困惑自己,我必须过度思考需要做些什么。抽象列表模型是使Jlist
正常工作所需的。我需要知道什么是对错。如果甚至需要使用getselectedvalues()
,我该把它放在哪里?
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);
答案 0 :(得分:2)
我建议使用DefaultListModel
,因为它可以改变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
访问它们。
在编写示例时,我认为编写一个可以将数据从一个列表移动到另一个列表的简单方法会很容易,例如protected void move(List<String> items, DefaultListModel from, DefaultListModel to)
,这意味着您只需使用move(Left_list.getSelectedValuesList(), Left_list.getModel(), Right_list.getModel())
或{ {1}}移动内容并减少代码重复...
move(Right_list.getSelectedValuesList(), Right_list.getModel(), Left_list.getModel())
实际上假设显示在JList
s中,这使得列表比屏幕上的可用空间更长/更宽
看看
...了解更多详情