Search code examples
javaobjectjpaneljbuttonuser-input

Add object for every user input


I am trying to create a new button every time the user presses a button.

So, if I click "add" button, then a new panel/button/thing would be added to the JPanel.

Not sure if this is the same thing I'm asking for

Don't know what

guiButtons[0]

is in

if(buttonClick.getSource().equals(guiButtons[0]))

Would be nice if someone could explain


Solution

  • You can try as bellow code, may it help you

    JPanel panel = new JPanel(new FlowLayout());
        JButton button = new JButton("Add");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton newButton = new JButton("New Added Bnt");
                panel.add(newButton);
            }
        });