0

I have a panel, and a button1. When I press the button, I want to create other buttons (of certain height and width) inside the panel. So I press the button1 once, it creates a button in the panel. I press button1 again, it creates another button in the panel right next to the other button or with some space in-between. Any help? I tried

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
           JButton jButton = new JButton("Button");
           panel1.add(jButton);
           validate();
        
    }   
GiGiK
  • 41
  • 4
  • Where do you want each extra button to appear on the panel? Do you want them in a row, one next to the other? Do you want them in a column, one under (or above) the other? Do you want them in a grid? Maybe you could post some kind of drawing or image that illustrates what you want? – Abra Sep 17 '20 at 16:33
  • Hello, I want the buttons to appear in a Row – GiGiK Sep 17 '20 at 17:55

2 Answers2

0

I presume you already have access to the panel considering you are trying to access it from inside the method.

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
            JButton button = new JButton("Button");
            button.setVisible(true);                 
            panel1.add(button);
            panel1.revalidate();
    }   

To choose a specific location for your button it all depends on what type of layout the JPanel has.

I believe the FlowLayout (which is the default layout for a JPanel) should place the button next to each other in a row until it's parent container has no more room horizontally according to it's parent, then it will start on the next row.

A guide to layouts by Oracle can be found Here

RStevoUK
  • 436
  • 4
  • 10
0

Here's a SSCCE that does what you desire.
It uses BoxLayout to ensure that the buttons are displayed in a single row.
It sets each button to the same, particular size.
There is the same size gap between each, adjacent button.
It uses a JScrollPane so that once you add more buttons that can fit into the width of the JScrollPane, you can scroll to see the other buttons. This is instead of increasing the size of the JPanel (or creating a new row) each time you add a button.

More explanations after the code.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

public class AdButton implements ActionListener, Runnable {
    private static final String  ADD = "Add";
    private static final String  EXIT = "Exit";

    private int  counter;
    private JFrame  frame;
    private JPanel  panel;

    @Override // java.awt.event.ActionListener
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case ADD:
                addButton();
                break;
            case EXIT:
                System.exit(0);
            default:
                JOptionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        createGui();
    }

    private void addButton() {
        panel.add(Box.createHorizontalStrut(10));
        JButton button = new JButton(String.valueOf(++counter));
        Dimension size = new Dimension(50, 30);
        button.setMaximumSize(size);
        button.setMinimumSize(size);
        button.setPreferredSize(size);
        panel.add(button);
        panel.revalidate();
    }

    private JButton createButton(String text, int mnemonic, String tooltip) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.setToolTipText(tooltip);
        button.addActionListener(this);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton(ADD, KeyEvent.VK_A, "Add button to panel"));
        buttonsPanel.add(createButton(EXIT, KeyEvent.VK_X, "Exit the application"));
        return buttonsPanel;
    }

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

    private JScrollPane createPanel() {
        panel = new JPanel();
        LayoutManager mgr = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        panel.setLayout(mgr);
        JScrollPane scrollPane = new JScrollPane(panel, 
                                                 ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
                                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(500, 100));
        return scrollPane;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new AdButton());
    }
}

You should use an ActionListener rather than a MouseListener when you want an action to be performed as a result of clicking in a JButton.

The important part in the addButton() method is the call to revalidate() which makes the JPanel layout all its components and draw them on the screen. You need to do this since you have added another component to the JPanel.

Abra
  • 19,142
  • 7
  • 29
  • 41