简体   繁体   中英

How to send data from a JTextField to a JList?

I have integrated a form to store the data from a JTextField to a variable after a user enters something and presses a JButton . My issue is that I want to send this data to a JList every time they type something in and hit the submit button.

How is this possible?

Add an ActionListener to the instance of the JButton, get the content of the JTextField (say with name aTextField), and store it to a list (say you have a class member JList dataInputted )

button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                // Execute when button is pressed
                dataInputted.addElement(aTextField.getText());
            }
        });  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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