Reputation: 5176
I have a JFrame
that has JPanel
inside. Inside JPanel
there are 2 buttons. JPanel
has a BoxLayout
. I need buttons to be displayed horizontally in the center of the window. Here is my code:
I just create two buttons, set their alignment to the center (tried all the ways I knew) and add them to the panel horizontally.
public class UserInterface extends JFrame {
public UserInterface() {
setup();
}
private void setup() {
...
panel=new UserInterfacePanel();
add(panel);
}
}
class UserInterfacePanel extends JPanel {
private JToggleButton startButton;
private JToggleButton stopButton;
public UserInterfacePanel() {
setup();
}
private void setup() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setupButtons();
setupButtonsActions();
add(startButton);
add(stopButton);
}
private void setupButtons() {
...
startButton.setHorizontalAlignment(JButton.CENTER);
stopButton.setHorizontalAlignment(JButton.CENTER);
startButton.setAlignmentX(CENTER_ALIGNMENT);
stopButton.setAlignmentX(CENTER_ALIGNMENT);
}
}
And yet it doesn't work. Why doesn't it and how to fix it?
Upvotes: 0
Views: 1569
Reputation: 324197
buttons are still located at the left side of the window.
If you want the components center horizontally on the line you need to use "glue"
add(Box.createHorizontalGlue());
add(startButton);
add(stopButton);
add(Box.createHorizontalGlue());
Upvotes: 2
Reputation: 285430
You're setting the x alignment of the buttons but you're box layout lays out along the x axis -- doesn't make sense. Either set the y alignment and use the x axis for the box layout direction, or set the x alignment and use the y axis for the box layout direction.
e.g.,
private void setup() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // **** change ****
setupButtons();
// setupButtonsActions();
add(startButton);
add(stopButton);
}
private void setupButtons() {
// ...
startButton.setHorizontalAlignment(JButton.CENTER);
stopButton.setHorizontalAlignment(JButton.CENTER);
startButton.setAlignmentX(CENTER_ALIGNMENT);
stopButton.setAlignmentX(CENTER_ALIGNMENT);
}
or
private void setup() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setupButtons();
// setupButtonsActions();
add(startButton);
add(stopButton);
}
private void setupButtons() {
// ...
startButton.setHorizontalAlignment(JButton.CENTER);
stopButton.setHorizontalAlignment(JButton.CENTER);
startButton.setAlignmentY(CENTER_ALIGNMENT); // **** change ****
stopButton.setAlignmentY(CENTER_ALIGNMENT); // **** change ****
}
To center all, use a different layout, such as a GridBagLayout:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
@SuppressWarnings("serial")
public class UserInterfacePanel extends JPanel {
private static final Insets INSETS = new Insets(3, 3, 3, 3);
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private JToggleButton startButton = new JToggleButton("Start");
private JToggleButton stopButton = new JToggleButton("Long Texted Title");
public UserInterfacePanel() {
setup();
}
private void setup() {
setLayout(new GridBagLayout()); // **** change
add(startButton, createGbc(0, 0));
add(stopButton, createGbc(1, 0));
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.insets = INSETS;
return gbc;
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
UserInterfacePanel mainPanel = new UserInterfacePanel();
JFrame frame = new JFrame("UserInterfacePanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Upvotes: 1