Hi there everyone,

Is it possible to use more than one Layout Manager in a single GUI interface? I have classes (with event handlers) for various buttons, jtext menus, etc... but so far I've only built simple programs that use a single layout manager. When I call the setLayout() method on my container object I can only specify 1 layout manager. What I'm wondering is if its possible to combine layout managers in a single GUI. The setLayout method only takes one layout manager as a parameter.

Many Thanks!

...Tyster

Each container can only have one layout manager, however you can easily create any layout you wish by nesting containers with the layout that you want for each one. This could include using a few JPanels inside a JFrame with each JPanel containing a few components. Breaking up the layout like that gives you a lot of control over the grouping and resizing behavior of complex layouts.

Each container can only have one layout manager, however you can easily create any layout you wish by nesting containers with the layout that you want for each one. This could include using a few JPanels inside a JFrame with each JPanel containing a few components. Breaking up the layout like that gives you a lot of control over the grouping and resizing behavior of complex layouts.

Thanks, Ezzaral!

Does anyone know where there might be a code sample for this sort of thing, just so I can get some guidance?

Many Thanks!

...Tyster

This should give you a pretty good idea on how to set them up.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class FrameExample extends JFrame {

    JPanel panNorth;
    JPanel panWest;
    JPanel panCenter;

    public FrameExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        // set up top panel
        panNorth = new JPanel();
        panNorth.setLayout(new FlowLayout());
        panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        JLabel lblUpper = new JLabel("Upper Panel");
        panNorth.add(lblUpper);
        add(panNorth, BorderLayout.NORTH);

        // set up left panel
        panWest = new JPanel();
        panWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        panWest.setLayout(new GridLayout(2, 1));
        JButton btnFirst = new JButton("First");
        panWest.add(btnFirst);
        JButton btnSecond = new JButton("Second");
        panWest.add(btnSecond);
        add(panWest, BorderLayout.WEST);

        // set up center panel
        panCenter = new JPanel();
        panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        panCenter.setLayout(new BorderLayout());
        JLabel lblMiddle = new JLabel("Middle thing");
        lblMiddle.setHorizontalAlignment(JLabel.CENTER);
        panCenter.add(lblMiddle, BorderLayout.CENTER);
        add(panCenter);

        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String args[]) {
        new FrameExample();
    }
}
This article has been dead for over six months. Start a new discussion instead.