Nicholas Mattiacci
Nicholas Mattiacci

Reputation: 65

Trouble with java layouts

I used to have a jframe with 2 components in it, each of them taking half of the width and all of the height: splitscreen1

I achieved this with a gridlayout with 2 columns and 1 row, which worked fine. Although now I'd like to add a third component beneath the one on the right: enter image description here

Does anybody know how I could achieve this?

Upvotes: 0

Views: 73

Answers (2)

Frakcool
Frakcool

Reputation: 11143

You can try nesting some JPanels, for example:

Have a main pane with a FlowLayout which will hold the left and right panes.

On the right pane have a BoxLayout or GridLayout to hold the top and bottom pane.

In the example code below which gives the following output

enter image description here

I added some border colors so you can see how it works, I added some labels because I'm too lazy to override the getPreferredSize() of each JPanel, but I think this will give you the idea on how to go on from here.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LayoutExample {
    private JFrame frame;
    private JPanel pane;
    private JPanel leftPane;
    private JPanel rightPane;
    private JPanel topPane;
    private JPanel bottomPane;
    public static void main (String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LayoutExample().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("Layout Example");
        pane = new JPanel();
        leftPane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300); 
            }
        };
        rightPane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300);
            }
        };
        topPane = new JPanel();
        bottomPane = new JPanel();

        pane.setLayout(new FlowLayout());
        rightPane.setLayout(new GridLayout(2, 1, 5, 5));

        rightPane.add(topPane);
        rightPane.add(bottomPane);

        pane.add(leftPane);
        pane.add(rightPane);
        pane.setBorder(BorderFactory.createLineBorder(Color.green));
        leftPane.setBorder(BorderFactory.createLineBorder(Color.red));
        topPane.setBorder(BorderFactory.createLineBorder(Color.black));
        bottomPane.setBorder(BorderFactory.createLineBorder(Color.magenta));

        frame.setContentPane(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Note: In order to get the same dimension on both, left and right panes in my example above, I had to override the getPreferredSize() method, that's because I used a FlowLayout, in this case, to avoid doing this it's better to use any of the 2 suggestions given by @LukkasRotter in his answer below.

Upvotes: 5

Lukas Rotter
Lukas Rotter

Reputation: 4188

Either use nested layouts, i.e. two grid layouts, or use GridBagLayout.

enter image description here

Example

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args) {
        new Example().createAndShowGUI();
    }

    public void createAndShowGUI() {
        JPanel nestedPanel = new JPanel(new GridLayout(2, 1, 10, 10));
        nestedPanel.add(createBorderedPanel());
        nestedPanel.add(createBorderedPanel());

        JPanel nestedPanelContainer = new JPanel(new GridLayout(1, 2, 10, 10));
        nestedPanelContainer.setBorder(BorderFactory.createTitledBorder("Nested GridLayouts"));
        nestedPanelContainer.add(createBorderedPanel());
        nestedPanelContainer.add(nestedPanel);

        JPanel gridBagPanel = new JPanel(new GridBagLayout());
        gridBagPanel.setBorder(BorderFactory.createTitledBorder("GridBagLayout"));
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.gridheight = 2;
        constraints.insets = new Insets(5, 5, 5, 5);
        gridBagPanel.add(createBorderedPanel(), constraints);
        constraints.gridheight = 1;
        constraints.gridx = 1;
        gridBagPanel.add(createBorderedPanel(), constraints);
        constraints.gridy = 1;
        gridBagPanel.add(createBorderedPanel(), constraints);

        JPanel contentPane = new JPanel(new GridLayout(1, 2, 5, 5));
        contentPane.add(nestedPanelContainer);
        contentPane.add(gridBagPanel);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(contentPane);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    private JPanel createBorderedPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        return panel;
    }

}

Upvotes: 5

Related Questions