Swing - 添加按钮JList
Swing - Add button JList
我必须为我的 Swing 项目设计一个 fleetmanagment 我创建了一个添加按钮,但我不知道必须制作一个删除按钮有什么帮助吗?这是我的 addbutton 代码。
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//make sure you preserve the previously selected list items
int size = rightlist.getModel().getSize();
Set objects = new LinkedHashSet();
for (int i = 0; i < size; i++) {
objects.add(rightlist.getModel().getElementAt(i));
}
objects.addAll(Arrays.asList(leftlistfreight.getSelectedValues()));
rightlist.setListData(objects.toArray());
}
});
编辑!
数组列表代码
List<FreightBoats> freightBoat = new ArrayList<FreightBoats>();
freightBoat.add(new FreightBoats("Boat Name : Stefan |","This Boat can Carry Conitainer : ",25000));
freightBoat.add(new FreightBoats("Boat Name : Sminroff |","This Boat can Carry Conitainer : ",30000));
freightBoat.add(new FreightBoats("Boat Name : Container 2000 |","This Boat can Carry Conitainer : ",2500));
freightBoat.add(new FreightBoats("Boat Name : Windows |","This Boat can Carry Conitainer : ",25200));
freightBoat.add(new FreightBoats("Boat Name : Unhuman |","This Boat can Carry Conitainer : ",200));
freightBoat.add(new FreightBoats("Boat Name : ElPolako |","This Boat can Carry Conitainer : ",300000));
freightBoat.add(new FreightBoats("Boat Name : BrainDead |","This Boat can Carry Conitainer : ",10000));
freightBoat.add(new FreightBoats("Boat Name : WSHR | ","This Boat can Carry Conitainer : ",34005));
freightBoat.add(new FreightBoats("Boat Name : Grolsch ","This Boat can Carry Conitainer : ",10565
不要玩 Arrays 或 ArrayLists。无需使用 setListData() 方法重新创建 ListModel。
应该直接对 ListModel
进行更新。
阅读 How to Use Lists 上的 Swing 教程部分。 ListDemo
示例向您展示了如何使用 "Hire" 和 "Fire" 按钮从 ListModel 获取 "add" 和 "remove" 项。
我必须为我的 Swing 项目设计一个 fleetmanagment 我创建了一个添加按钮,但我不知道必须制作一个删除按钮有什么帮助吗?这是我的 addbutton 代码。
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//make sure you preserve the previously selected list items
int size = rightlist.getModel().getSize();
Set objects = new LinkedHashSet();
for (int i = 0; i < size; i++) {
objects.add(rightlist.getModel().getElementAt(i));
}
objects.addAll(Arrays.asList(leftlistfreight.getSelectedValues()));
rightlist.setListData(objects.toArray());
}
});
编辑!
数组列表代码
List<FreightBoats> freightBoat = new ArrayList<FreightBoats>();
freightBoat.add(new FreightBoats("Boat Name : Stefan |","This Boat can Carry Conitainer : ",25000));
freightBoat.add(new FreightBoats("Boat Name : Sminroff |","This Boat can Carry Conitainer : ",30000));
freightBoat.add(new FreightBoats("Boat Name : Container 2000 |","This Boat can Carry Conitainer : ",2500));
freightBoat.add(new FreightBoats("Boat Name : Windows |","This Boat can Carry Conitainer : ",25200));
freightBoat.add(new FreightBoats("Boat Name : Unhuman |","This Boat can Carry Conitainer : ",200));
freightBoat.add(new FreightBoats("Boat Name : ElPolako |","This Boat can Carry Conitainer : ",300000));
freightBoat.add(new FreightBoats("Boat Name : BrainDead |","This Boat can Carry Conitainer : ",10000));
freightBoat.add(new FreightBoats("Boat Name : WSHR | ","This Boat can Carry Conitainer : ",34005));
freightBoat.add(new FreightBoats("Boat Name : Grolsch ","This Boat can Carry Conitainer : ",10565
不要玩 Arrays 或 ArrayLists。无需使用 setListData() 方法重新创建 ListModel。
应该直接对 ListModel
进行更新。
阅读 How to Use Lists 上的 Swing 教程部分。 ListDemo
示例向您展示了如何使用 "Hire" 和 "Fire" 按钮从 ListModel 获取 "add" 和 "remove" 项。