Youssef Ahmed
Youssef Ahmed

Reputation: 39

How to make a JButton that will create other buttons in java Swing?

I am creating a user system to hold multiple details of multiple users, so I would like to create a button that would be able to create another button. When the second button is pressed a form will open for the user to fill. I have already created the form for the user to fill but I cannot manage to make the button to create more buttons to work. I have coded this but it does not show the button on the JPanel.

I have created the following code:

private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {    
    JButton b=new JButton("Click Here"); 
    b.setBounds(50,100,95,30);  
    jPanel3.add(b); 
    b.setVisible(true); 
}

I want to know what is the correct code to write in the events / mouseClick of the button.

Upvotes: 0

Views: 160

Answers (1)

Abra
Abra

Reputation: 20914

When you add or remove components from a JPanel, you need to make that JPanel redraw itself. Just adding or removing a component does not make this happen. Hence, after adding or removing a component from a JPanel, you need to call method revalidate followed by a call to repaint.

Refer to Java Swing revalidate() vs repaint()

Also note that the following line of your code is not required since the visible property is true by default.

b.setVisible(true);

Also, it is recommended to use a layout manager which means you don't need to call method setBounds as you have in this line of your code.

b.setBounds(50,100,95,30);

EDIT

As requested, a sample application. Clicking on the Add button will add another button. Note that the ActionListener for the Add button is implemented as a method reference.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonAd {
    private static final String  ADD = "Add";

    private JFrame  frame;
    private JPanel  buttonsPanel;

    private void addButton(ActionEvent event) {
        JButton button = new JButton("Added");
        buttonsPanel.add(button);
        buttonsPanel.revalidate();
        buttonsPanel.repaint();
    }

    private JPanel createAddButton() {
        JPanel addButtonPanel = new JPanel();
        JButton addButton = new JButton(ADD);
        addButton.addActionListener(this::addButton);
        addButtonPanel.add(addButton);
        return addButtonPanel;
    }

    private void createAndDisplayGui() {
        frame = new JFrame("Add Buttons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createButtonsPanel(), BorderLayout.CENTER);
        frame.add(createAddButton(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtonsPanel() {
        buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        buttonsPanel.setPreferredSize(new Dimension(450, 350));
        return buttonsPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new ButtonAd().createAndDisplayGui());
    }
}

Upvotes: 2

Related Questions