简体   繁体   English

按JButton时JList不更新

[英]JList not updating on JButton press

i'm trying to get what's entered into the JTextField on button press, then store that in a string which I then use to update my JList. 我试图在按钮按下时获取输入到JTextField中的内容,然后将其存储在一个字符串中,然后使用该字符串来更新我的JList。 The only problem is that when I hit the button press nothing happens, yet the testing text I tried appeared perfectly fine. 唯一的问题是,当我按下按钮时,什么也没发生,但是我尝试过的测试文本看起来很好。 Anyone have any ideas? 有人有想法么? Cheers. 干杯。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;


public class MainPanel {
    JList list;
    JTextField pushTextEntry;
    Stack lifoStack = new Stack();
    String pushTextListener;
    Boolean b = false;
    String a;

    JComponent mainPanel(final JFrame newFrame) throws IOException {


        ////////////////////////////////////////////////////////////
        final GridBagConstraints gbLayout = new GridBagConstraints();
        final JComponent panel = new JLabel();
        panel.setLayout(new GridBagLayout());

        gbLayout.weightx = 1.0;
        gbLayout.weighty = 1.0;
        gbLayout.gridx = 1;
        gbLayout.gridy = 1;
        gbLayout.anchor = GridBagConstraints.SOUTHEAST;

        gbLayout.insets = new Insets(15,15,215,45); 
        JButton Push = new JButton("Push");
        panel.add(Push, gbLayout);

        pushTextEntry = new JTextField(5);
        gbLayout.insets = new Insets(15,15,215,120);    
        panel.add(pushTextEntry,gbLayout);

        gbLayout.insets = new Insets(15,15,130,45);
        JButton Pop = new JButton("  Pop  ");
        panel.add(Pop, gbLayout);

        gbLayout.insets = new Insets(15,15,40,45);
        JButton Reset = new JButton("Reset");
        panel.add(Reset, gbLayout);
        ////////////////////////////////////////////////////////////

        Push.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                 for (int i = 1; i <= 10; i++)
                    {     
                        pushTextListener = pushTextEntry.getText();

                        lifoStack.add(pushTextListener);
                    }
            }
        });

        String listData[] = {
                "test",
                pushTextListener
        };

        list = new JList(listData);
        gbLayout.insets = new Insets(60,15,160,140);
        panel.add(list,gbLayout);

        return panel;
    }

}

You shoud use the DefaultListModel to update the data. 您应该使用DefaultListModel更新数据。 The ListModel will notify the JList when it changes and the JList will update automatically. ListModel更改时, ListModel将通知JList ,并且JList将自动更新。

final DefaultListModel listModel = new DefaultListModel();
listModel.addElement("test");
list = new JList(listModel);

Push.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        for (int i = 1; i <= 10; i++) {
            pushTextListener = pushTextEntry.getText();
            listModel.addElement(pushTextListener);
        }
    }
});

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

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