简体   繁体   English

更新/重新创建JList

[英]Updating/recreating a JList

This is my first post here, and I am very green with Java. 这是我在这里的第一篇文章,并且我对Java非常满意。 This is something I'm trying to make to improve my java knowledge. 这是我要尝试改善的Java知识。

I have a button, which when clicked produces a shuffled card deck as a Jlist. 我有一个按钮,单击该按钮会产生一个经过洗牌的卡片组作为Jlist。 When pressed again, I would very much like it to refresh the JList, or recreate it somehow. 再次按下时,我非常希望它刷新JList或以某种方式重新创建它。 Instead, it simply creates a new list, so I now have 2 JLists. 相反,它只是创建了一个新列表,所以我现在有2个JList。

        button1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {

            cards.choseCards(); //Creates an ArrayList with the suffled cards
            JList<String> displayList = new JList<>(cards.deck.toArray(new String[0]));
            frame.add(displayList);
            frame.pack();
            cards.cleanUpDeck(); //Removes all the cards from the ArrayList
        }
    });

The key here is that Swing uses a model-view type of structure similar to model-view-controller (but with differences) where the model holds the data that the view (the component) displays. 此处的关键是Swing使用类似于模型视图控制器的模型视图类型的结构(但有区别),其中模型保存视图(组件)显示的数据。

What you are doing is creating an entirely new JList, but what you want to do is to update the model of the existing and displayed JList, either that or create a new model for this same existing JList. 您正在做的是创建一个全新的JList,但是您想要做的是更新现有的和显示的JList的模型 ,或者为该相同的现有JList创建新模型。 JLists use a ListModel for their mode, often implemented as a DefaultListModel object, and so you will want to update or replace this model such as by creating a new DefaultListModel object and then inserting it into the existing JList by calling its setModel(ListModel model) method. JList将ListModel用于其模式,通常实现为DefaultListModel对象,因此您将希望更新或替换此模型,例如通过创建一个新的DefaultListModel对象,然后通过调用其setModel(ListModel model)将其插入到现有的JList中。方法。

For example your code could look something like this (made with lots of guesses since we don't know what your real code looks like): 例如,您的代码可能看起来像这样(进行了很多猜测,因为我们不知道您的真实代码是什么样子):

button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // create new model for the JList
        DefaultListModel<String> listModel = new DefaultListModel<>();
        cards.choseCards(); //Creates an ArrayList with the suffled cards

        // add the cards to the model. I have no idea what your deck field looks like
        // so this is a wild guess.
        for (Card card : cards.deck) {
            listModel.addElement(card.toString());  // do you override toString() for Card? Hope so
        }

        // Guessing that your JList is in a field called displayList.
        displayList.setModel(listModel);  // pass the model in
        cards.cleanUpDeck(); //Removes all the cards from the ArrayList
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM