Search code examples
javaswinguser-interfacejbuttonsoftware-design

Creating a JButton with the click of a JButton


I am working on a project where I need to click a button to make another button. Eventually, I would l like to have more control over the new button positioning and being able to create new ones multiple times, but for now...I am looking to just make one JButton Create another JButton.

With the code below, my goal is to have the White B1 create a Red B3 button. I also want the Blue B2 button to create a Green B4 buttion.

Eventually, I also want the B3 and B4 (buttons generated from buttons) to have the user be able to click those and make it disappear.

Neither button seems to do anything and I can't figure out why. I have 3 class files. Any idea where I may be going wrong?

Window.Java

package gui;

import javax.swing.JFrame;

public class Window {

public static void main(String[] args) {

    //frame creation
    JFrame frame = new MainFrame("Button Create Button Test");

    //frame size
    frame.setSize(800, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
}

DetailsPanel.Java

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DetailsPanel extends JPanel {
    public DetailsPanel() {
        Dimension size = getPreferredSize();
        size.width = 400;
        setPreferredSize(size);

        ///Buttons

        JButton button1 = new JButton("B1");
        button1.setPreferredSize(new Dimension (72, 73));
        button1.setBackground(Color.WHITE);
        button1.setBorderPainted(true);

        JButton button2 = new JButton("B2");
        button2.setPreferredSize(new Dimension (72, 73));
        button2.setBackground(Color.BLUE);
        button2.setBorderPainted(true);


        setLayout (new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        /// Layout ///
        /// Row 1 ///

        gc.anchor = GridBagConstraints.NORTH;

        gc.weightx = 0.5;
        gc.weighty = 0.5;

        gc.gridx = 1;
        gc.gridy = 1;

        add(button1, gc);

        gc.gridx = 1;
        gc.gridy = 2;

        add(button2, gc);

        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button3 = new JButton("B3");
                button3.setPreferredSize(new Dimension (72, 73));
                button3.setBackground(Color.RED);
                button3.setBorderPainted(true);

                gc.gridx = 1;
                gc.gridy = 3;

                add(button3, gc);
            }
        });

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button4 = new JButton("B3");
                button4.setPreferredSize(new Dimension (72, 73));
                button4.setBackground(Color.GREEN);
                button4.setBorderPainted(true);

                gc.gridx = 1;
                gc.gridy = 4;

                add(button4, gc);
            }
        });
    }
}

MainFrame.Java

    package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class MainFrame extends JFrame {

    private DetailsPanel detailsPanel;

    public MainFrame(String title) {
        super(title);

        // set layout manager
        setLayout (new BorderLayout());

        // Create Swing Component
        detailsPanel = new DetailsPanel();
        detailsPanel.setBackground(Color.BLACK);

        // Add swing components to content pane
        Container container = getContentPane();

        container.add(detailsPanel, BorderLayout.WEST);



            }       
    }

Solution

  • In order for button B3 to be displayed after clicking button B1, you need to add...

    revalidate();
    repaint();
    

    after the line...

    add(button3, gc);
    

    in file DetailsPanel.java.
    Similarly for button B2.