Search code examples
javaswinguser-interfacejframejtextfield

JFrame: How to add a button that adds a new text field to the frame?


I'm pretty new to Java and I thought I'd try to get my hands dirty and make a GUI but I can't seem to get it to work the way I want it to.

I wrote some code thinking that if I press the "Add" button on the GUI then a new JTextField will appear underneath where all the other textfields are but that doesn't happen. Only one new JTextField does appear but it appears next to my Add button instead of underneath all the other textfields I have and if I press it again, nothing happens. I tried playing around with other variables but it just doesn't seem to be working properly. I feel like something is wrong with my ActionListener but I don't know what.

public class TheGUI extends JFrame{
    List<JTextField> listOfTextFields = new ArrayList<JTextField>();

    private JTextField desc1;
    private JTextField instruct;
    private JTextField desc2;
    private JButton submit;
    private JButton addNew;

    public TheGUI() { //My GUI with the default fields & buttons that should be on there.
        super("Chili");
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        instruct = new JTextField("Choose your words");
        instruct.setEditable(false);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        add(instruct, c);

        addNew = new JButton("Add");
        c.weightx = 0.0;
        c.gridx = 1;
        c.gridy = 0;
        add(addNew, c);

        submit = new JButton("Submit!");
        c.weightx = 0.5;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = GridBagConstraints.PAGE_END;
        add(submit, c);

        desc1 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 1;
        add(desc1, c);

        desc2 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 2;
        add(desc2, c);

        addNew.addActionListener(new Adder());


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);

    }

    private class Adder implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {

            int i = 0;
            listOfTextFields.add(new JTextField());
            GridBagConstraints textFieldConstraints = new GridBagConstraints();

             //Give it a max of 9 text fields that can be created.
            while(i < 10) {
                textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                textFieldConstraints.weightx = 0.5;
                textFieldConstraints.gridx = 0;
                textFieldConstraints.gridwidth = 2;
                textFieldConstraints.gridy = 3 + i;
                i++;

            }

            add(listOfTextFields.get(i), textFieldConstraints);
            revalidate();
            repaint();


        }

    }


}


Solution

  • Your while loop is really strange.

    Your ActionListener should look like:

     private class Adder implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent event) {
                if (listOfTextFields.size() == 9) {
                    // Give it a max of 9 text fields that can be created.
                    return;
                }
    
                JTextField textfield = new JTextField();
    
                listOfTextFields.add(textfield);
                GridBagConstraints textFieldConstraints = new GridBagConstraints();
                textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                textFieldConstraints.weightx = 0.5;
                textFieldConstraints.gridx = 0;
                textFieldConstraints.gridwidth = 2;
                textFieldConstraints.gridy = 3 + listOfTextFields.size();
    
                add(textfield, textFieldConstraints);
    
                revalidate();
                repaint();
    
            }
    
        }