views:

145

answers:

2

Is there way to use more than 1 layout manager in Java. Right now I'm using a gridLayout to implement a chess board but beneath it I would like to put some other stuff but not in a gridLayout. Maybe a FlowLayout or some other layout. How would I go about doing this? Thanks!

+2  A: 

Is there way to use more than 1 layout manager in Java.

Absolutely. In fact, using multiple layout managers is the norm.

How would I go about doing this?

Any Container subclass can have a LayoutManager and contain child elements. And each of these child elements can itself be a Container with children. The most commonly used container below the top-level frames is JPanel.

For your example, you should probably use a BorderLayout for the frame, put a JPanel with the grid in its CENTER position (because that's the one that gets all available remaining space when the other positions have been given their preferred sizes) and another JPanel with the "other stuff" in the SOUTH position.

More details can be found in the Swing tutorial on layout managers.

Michael Borgwardt
+3  A: 

Yes, all you need is to plan your over all UI Layout (i.e; Window, master panel etc)

For example, you need to put something under your chessboard, I would normally go with a BorderLayout at the basic level.

So assume I have a JPanel called masterPanel, that holds all the components for my chess app. So, code would look like:

JPanel masterPanel = new JPanel(new BorderLayout());
JPanel chessBoardPanel = createChessboardPanel(); //assuming this method will return a
//JPanel with chess board using GridLayout
JPanel infoPanel = new JPanel(); //this is the panel that would contain info elements, that //may go below my chess board.
//Now add everything to master panel.
masterPanel.add(chessBoardPanel, BorderLayout.CENTER);
masterPanel.add(infoPanel, BorderLayout.PAGE_END);
//add masterPanel to your window (if required)
this.getContentPane().add(masterPanel);
ring bearer