Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Layout Managers in Java

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!

like image 970
ranzy Avatar asked Mar 31 '10 16:03

ranzy


People also ask

Can a program have multiple layout managers?

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

How many layout managers are available in Java?

The java. awt package provides five layout managers: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout.

What are different layout managers in Java?

Several AWT and Swing classes provide layout managers for general use: BorderLayout. BoxLayout. CardLayout.

How many layout managers can a container have?

A container can have only one layout manager but may also contain components that each have a different layout manager.


1 Answers

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);
like image 162
ring bearer Avatar answered Oct 06 '22 00:10

ring bearer