簡體   English   中英

搖擺-添加按鈕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());
        }
    });

編輯!

ArrayList代碼

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進行更新。

閱讀Swing教程中有關如何使用列表的部分 ListDemo示例向您展示如何通過使用“ Hire”和“ Fire”按鈕從ListModel中“添加”和“刪除”項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM