简体   繁体   English

在Swing中向模型添加元素后,JList不会更新

[英]JList doesn't update after adding elements to the Model in Swing

I'm having troubles updating a JList widget in Swing. 我在更新Swing中的JList小部件时遇到麻烦。

I'm creating a model, adding elements to it, then setting it as model for a JList element. 我正在创建一个模型,向其中添加元素,然后将其设置为JList元素的模型。 Everything works well, it's showed correctly. 一切正常,显示正确。

The problem is when I want to add an element to the model. 问题是当我想向模型添加元素时。 It doesn't do anything. 它什么也没做。

I'm adding elements on the fly after an update from an Observer Pattern is fired. 触发观察者模式的更新后,我正在动态添加元素。

What am I doing wrong ? 我究竟做错了什么 ?

Here is the code of the UI. 这是UI的代码。

// all the imports

    public class View extends JPanel implements Observer{
        private vivo vivox;
        private Label label = new Label("Hola Mundo");
        private DefaultListModel modelo = new DefaultListModel();
        private JList nameList = new JList(modelo);


        public View() {
            super();
            nameList.setFont(new Font("Arial",Font.BOLD,40));
            this.setLayout(new GridLayout());
            JButton button = new JButton("New Time");
            JButton button1 = new JButton("New Time2");
            label.setFont(new Font("Serif", Font.PLAIN, 50));
            JPanel group = new JPanel( new GridLayout());
            group.add(button);
            group.add(button1);

            JPanel east = new JPanel( new BorderLayout() );
            east.add(label, BorderLayout.NORTH);
            east.add(nameList, BorderLayout.CENTER);
            east.add(group, BorderLayout.SOUTH);

            add(east, BorderLayout.EAST);
            vivox = new vivo();
            vivox.addObserver(this);
            this.setListElements();
        }

        public void setListElements() {
            this.modelo.addElement("Messi");
            this.modelo.addElement("CR7");
            this.modelo.addElement("Iniesta");
        }

        @Override
        public void update(Observable o, Object arg) {
            this.label.setText("Some changes");
            this.modelo.addElement("Maradona");
        }


    }

The problem must be either with layout (that you dont see the actual happening changes) or is somewhere else as this shoult work. 问题可能出在布局上(您看不到实际发生的更改),还是在其他地方(应进行此工作)。 Here is working example that can show you, when adding new elements to list may be not visible (due to element size restrictions or actual size). 这是可以向您展示的工作示例,当向列表添加新元素时可能不可见(由于元素大小限制或实际大小)。

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    DefaultListModel<String> model = new DefaultListModel<>();
    for (int i = 0; i < 5; i++) {
        model.addElement(String.valueOf(Math.random()));
    }
    JList list = new JList(model);
    JButton button = new JButton("Click me!");
    button.addActionListener(e -> model.addElement(String.valueOf(Math.random())));

    frame.add(list);
    frame.add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
} 

This is as simple as that. 就这么简单。 Notice that when you add couple elements you wont see the changes because they exceeds the frame, and you have to resize it. 请注意,当添加几个元素时,您将看不到更改,因为它们超出了框架,因此您必须调整其大小。 I think that similar issue may be in your case, or update code is not invoked at all. 我认为您可能遇到类似问题,或者根本没有调用更新代码。

PS: This is Java 8 example (used lambda - use anonymous ActionListener extending class in version<8 environements) PS:这是Java 8示例(使用的lambda-在版本<8环境中使用匿名ActionListener扩展类)

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

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