4

I would like to be able to have three JPanels p1 p2 and p3, and have them lay out like so:

Layout
I have been playing around with FlowLayout, BoxLayout etc but I'm not really sure if I am heading in the right direction. I am quite new with Java so I don't know what does what if I'm quite honest.

I like how BoxLayout works, resizing the panels, but I would like to be able to give it some sort of width attribute.

I am not using a visual designer for this, this is my window code at the moment:

private void initialize() {
    Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();

    frame = new JFrame();
    frame.setLayout(new GridLayout(1, 3));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
            dimensions.height / 2 - WINDOW_HEIGHT / 2, 
            WINDOW_WIDTH, WINDOW_HEIGHT);

    JPanel p1 = new JPanel(new BorderLayout());
    p1.setBackground(Color.red);

    JPanel p2 = new JPanel(new BorderLayout());
    p2.setBackground(Color.black);  

    JPanel p3 = new JPanel(new BorderLayout());
    p3.setBackground(Color.blue);       

    frame.add(p2);

    frame.add(p1);  
    frame.add(p3);
}

Any pointers appreciated!

EDIT: I have managed to get it to work how I wanted, thanks to mKorbel. The right column isn't laid out exactly as I was going to do it but I actually changed my mind and decided to keep the other layout.

PanelWindow
The code:

import java.awt.*;
import javax.swing.*;

public class PanelWindow extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final int WINDOW_HEIGHT = 600;
    private static final int WINDOW_WIDTH = 800;
    private JPanel p1, p2, p3;

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                PanelWindow panelWindow = new PanelWindow(); 
            }
        });
    }

    public PanelWindow() {
        initialize();
    }

    private void initialize() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        p1 = new JPanel();
        p1.setBackground(Color.red);
        p2 = new JPanel();
        p2.setBackground(Color.green);
        p3 = new JPanel();
        p3.setBackground(Color.blue);

        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 97;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p1, gbc);

        gbc.gridy = 1;
        gbc.weightx = gbc.weighty = 3;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p2, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.weightx = 20;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(p3, gbc);

        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
            dimensions.height / 2 - WINDOW_HEIGHT / 2, 
            WINDOW_WIDTH, WINDOW_HEIGHT);

        setTitle("Panel Window");
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
  • What is the content of the various panels (specifics please)? If pure graphics, set a scale factor in paint. If components, a 'divide by percentage' seems wrong. – Andrew Thompson Jun 29 '12 at 11:40
  • The contents will be components, most likely JLabels. – Adam K Dean Jun 29 '12 at 12:16
  • @Andrew Thompson please why not percentage, todays & custom LayoutManagers are based on proportional ratios instead of (Standard LayoutManager) to take the PreferredSize from containers childs – mKorbel Jun 29 '12 at 13:00
  • @mKorbel I may have used words that were stronger than I intended. While this type of layout might be useful, it is ..unusual to me. – Andrew Thompson Jun 29 '12 at 13:19
  • @Andrew Thompson I have only provoked, here is only a few good (Swing & LayoutManager) answerers (it isn't my case) and only one of them we can to call VanGUI_ph:-) .... – mKorbel Jun 29 '12 at 13:23
  • @mKorbel Pfft.. No false modesty, please. :) Looking at top posters for [Swing](http://stackoverflow.com/tags/swing/topusers) & [layout manager](http://stackoverflow.com/tags/layout-manager/topusers) shows that while I top the latter, you top the former. I tip my hat. Your answer on this thread was good, too. – Andrew Thompson Jun 29 '12 at 13:55
  • @Andrew Thompson this talking about one of posters with `many posts` and `poor up_vote ratio` (my bad I'm by default < half of answers > going to answering non_answerable question) but `your and HFOE honesty` have got real acceptable `posters ratio => 3`, – mKorbel Jun 29 '12 at 15:02

2 Answers2

7

not my favorite LayoutManager, example by using GridBagLayout, easiest could be to use MigLayout, maybe ...

enter image description hereenter image description here

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        setLayout(new GridBagLayout());// set LayoutManager
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();

        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        add(panel1, gbc); // add compoenet to the COntentPane

        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(panel2, gbc); // add component to the COntentPane

        JPanel panel3 = new JPanel();
        panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
        gbc.gridx =1;
        gbc.gridy = 0;
        gbc.gridwidth = /*gbc.gridheight = */1;
        gbc.gridheight = 2;
        gbc.weightx = /*gbc.weighty = */20;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(panel3, gbc);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
        pack();
        setVisible(true); // important
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() { // important

            public void run() {
                BorderPanels borderPanels = new BorderPanels();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • This worked great, thanks, there is just one issue - there is a thin 1px grey line on the right and bottom border of the panels inbetween the window border and the inner content. I've removed the insets but there is still a little 1px border. I've set the window background - to no avail. Any ideas? http://i.imgur.com/aA2ua.png - I don't think it's related to GridBagLayout primarily, it also appears whilst using GridLayout, just only on the right. Hmm. – Adam K Dean Jun 29 '12 at 13:03
  • Ignore the above, I had to get the content pane, `getContentPane().setBackground(Color.black);` – Adam K Dean Jun 29 '12 at 13:08
1

One word: MigLayout. As you have seen, it is possible with the standard layout managers. But I can guarantee you, the layout code would be reduced to 4 lines with MigLayout... (Although it would take you some time to get into MigLayout as it works a bit different than the usual layout managers).

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • I was looking around SO before and found a reference to this. Saw you put it here and I've been playing with it - decided it actually works better for me. Using GridBagLayout caused issues with resizing but I much prefer the way MigLayout works, thanks! – Adam K Dean Jun 29 '12 at 19:47