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
.