Search code examples
javaswingjtextareajlist

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?


Solution

  • 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());
                }
            });