-
Alignment in GridLayout
I have a few nested panels and I want to center align the contents of the cells.
An example of what I have is:
Code:
Container witIncrementPane = new Panel();
witIncrementPane.setLayout(new GridLayout (1,3,5,1));
final JButton witUpButton = new JButton(upArrow);
witUpButton.setBackground(Color.white);
final JButton witDownButton = new JButton(downArrow);
witDownButton.setBackground(Color.white);
witIncrementPane.add(witUpButton);
witIncrementPane.add(witIncrementLabel);
witIncrementPane.add(witDownButton);
I want the buttons and label to all be centered.
-
(I think) In a gridlayout, buttons will take up the entire space of the cell they are in therefore making it impossible to center them. To center text of a JLabel: Code:
JLabel label = new JLabel("text", JLabel.CENTER);
-
You could use greater vgap and hgap values, with GridLayout.
Or, use a Box with combination of GridLayout(1,1). I've used this trick:
Code:
Box buttonBox = Box.createHorizontalBox();
buttonBox.createHorizontalGlue();
buttonBox.add(cancelButton);
buttonBox.add(new JPanel()); // dummy
buttonBox.add(backButton);
buttonBox.add(nextButton);
container.add(buttonBox);