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!
Absolutely. In fact, using multiple layout managers is the norm.
The java. awt package provides five layout managers: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout.
Several AWT and Swing classes provide layout managers for general use: BorderLayout. BoxLayout. CardLayout.
A container can have only one layout manager but may also contain components that each have a different layout manager.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With