how to center JPanel vertically
I`d like to align a JPanel to center. I`ve tried BorderLayout but I don`t want this JPanel to be forcefully extended - I want it to stay the way it is.
In this JPanel I have a few buttons that create a kind of menu and I want this menu to be in the center of the Frame. In the future I`ll probablt want to add some stuff at the bottom, top, left and right, so BorderLayout seems the perfect solution. The problem is that I don`t want the buttons to be resized to fill out the whole center area.
I`ve made a quick layout of what I mean here:
http://ilozen.republika.pl/stuff/layout.GIF
[610 byte] By [
Beholdera] at [2007-10-3 4:35:51]

The component you add to the center of a BorderLayout is resized to fit the entire area. By nesting panels, only the outer panel is resized, you can achieve the desired effect:
JPanel center = new JPanel( new FlowLayout() );
center.add( yourButtonPanel );
getContentPane().add( center );
Here`s my code
public class MainMenu extends JPanel{
private static final long serialVersionUID = Adventurers.getVersion();
private JFrame parentFrame;
private JPanel buttonsPanel;
private JPanel buttons;
private JButton newGameButton;
private JButton loadGameButton;
private JButton optionsButton;
private JButton exitButton;
private final int numberOfButtons = 4;
public MainMenu(JFrame parent) {
parentFrame = parent;
buttons = new JPanel();
newGameButton = new JButton("NEW GAME");
loadGameButton = new JButton("LOAD GAME");
optionsButton = new JButton("OPTIONS");
exitButton = new JButton("EXIT");
exitButton.addActionListener(new ExitListener(parentFrame) );
buttons.setLayout( new GridLayout(numberOfButtons,1));
buttons.add(newGameButton);
buttons.add(loadGameButton);
buttons.add(optionsButton);
buttons.add(exitButton);
buttonsPanel = new JPanel(new FlowLayout());
buttonsPanel.add(buttons);
setLayout(new BorderLayout());
add(buttonsPanel);
}
}
And in my GameWindow (extends JFrame) class I do something like this
getContentPane().add(new MainMenu(this));
The effect is that the buttons are at the top of the frame, like it there would be no Layout set. However if I add 'buttons' Instead of 'buttonsPanel' to MainMenu (last line of code) I get all the buttons resized to fit the whole frame. So BorderLayout works here, but in some strange way....?
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
> I get all the buttons resized to fit the whole frame.
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url].
GridLayout expands to fill the entire area and each cell is made the same size. Maybe you can use a BoxLayout instead.
We can't give an exact solution given general information we have available to use.
Here`s the full code in one file that shows the problem.
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Problem {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
private GameWindow gameWindow;
public void run() {
gameWindow = new GameWindow();
}
});
}
}
class GameWindow extends JFrame{
public GameWindow() {
setExtendedState(MAXIMIZED_BOTH);
getContentPane().add(new MainMenu(this));
setVisible(true);
}
}
class MainMenu extends JPanel{
private JFrame parentFrame;
private JPanel buttonsPanel;
private JPanel buttons;
private JButton newGameButton;
private JButton loadGameButton;
private JButton optionsButton;
private JButton exitButton;
private final int numberOfButtons = 4;
public MainMenu(JFrame parent) {
parentFrame = parent;
buttons = new JPanel();
newGameButton = new JButton("NEW GAME");
loadGameButton = new JButton("LOAD GAME");
optionsButton = new JButton("OPTIONS");
exitButton = new JButton("EXIT");
buttons.setLayout( new GridLayout(numberOfButtons,1));
buttons.add(newGameButton);
buttons.add(loadGameButton);
buttons.add(optionsButton);
buttons.add(exitButton);
buttonsPanel = new JPanel(new FlowLayout());
buttonsPanel.add(buttons);
setLayout(new BorderLayout());
add(buttonsPanel);
}
}
> Here`s the full code in one file that shows the problem.
That the way the LayoutManagers work, which is why you need to read the tutorial.
The FlowLayout, by default centers component horizontally, not vertically.
When you add a panel using a GridLayout directly to the center then all cells are made an equal size for the total space available.
> how to center JPanel vertically
Must admit I missed the verticaly part initially. There are a couple of ways to do this.
a) use a vertical BoxLayout. Add a glue component, then your component then some more glue.
b) vertical and horizontal centering of a component is the default behaviour of the GridBayLayout. So this may be the easier solution.
//buttonsPanel = new JPanel(new FlowLayout());
buttonsPanel = new JPanel(new GridBagLayout());
//buttonsPanel.add(buttons);
buttonsPanel.add(buttons, new GridBagConstraints());
setLayout(new BorderLayout());
add(buttonsPanel);
You still need to read the tutoral to be aware of the basic bahaviour of each LayoutManager so you can mix and match in the future.