4

I can't get my 3 panels to align properly. I basically have 1 big panel and 2 smaller (not necesarily identical) ones. The big one is on the left and the 2 smaller ones on the right, with one above the other. The components should remain dynamically resizeable.

What I want is this( 9's is the big one, 1 is one of the small one, 2 is the other small one):

999999 111
999999 111
999999 111
999999 222
999999 222

What I get is this:

999999 111
999999 111
999999 111
999999 
999999 
       222
       222

My code is as follows with graphsimscrollpane big one and workpanel and informationpanel the smaller ones:

    private void createLayout(GroupLayout groupLayout) {
    groupLayout.setHorizontalGroup(
            groupLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(graphSimScrollPane,  0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(groupLayout.createParallelGroup(Alignment.CENTER)
                        .addComponent(workPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(informationPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createSequentialGroup()
                .addGroup(groupLayout.createParallelGroup(Alignment.CENTER)
                    .addComponent(graphSimScrollPane,  0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(workPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addComponent(informationPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        guiFrame.getContentPane().setLayout(groupLayout);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sven
  • 1,133
  • 1
  • 11
  • 22
  • Is there a reason you can't nest these? In android, which is very similar to swing, I would accomplish this using a horizontal linear layout with the "9999" view on the left, then a vertical layout on the right with the "111" and "222" views – amarunowski Feb 14 '13 at 14:30
  • 1
    I think I could do that, however, I prefer to use the grouplayout because of the resizeability and because it is easy to use in Windowbuilder, the tool I use to help me make this GUI. – Sven Feb 14 '13 at 14:36

2 Answers2

4

The basic structure of your GroupLayout would be similar to this code :

    JPanel pnl = new JPanel();
    GroupLayout l = new GroupLayout(pnl);
    pnl.setLayout(l);

    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");

    l.setHorizontalGroup(
            l.createSequentialGroup()
                .addComponent(b1)
                .addGroup(l.createParallelGroup()
                    .addComponent(b2)
                    .addComponent(b3)));

    l.setVerticalGroup(
            l.createParallelGroup()
                .addComponent(b1)
                .addGroup(l.createSequentialGroup()
                    .addComponent(b2)
                    .addComponent(b3)));

    JFrame f = new JFrame("test");
    f.setContentPane(pnl);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(1024, 768);
    f.setVisible(true);

In plain english, the constraint on the horizontal axis reads "b1, then b2 and b3 in parallel". The constraint on the vertical axis reads "b1 in parallel with a group composed of b1 then b2".

Obviously, the resizability properties are mising, here. You need to give more details on how you want the component to behave regarding their size.

One possibility would be to give the two small components the whole vertical space, but to give them only their prefered horizontal space. And of course, give the big component the remaining space.

    l.setHorizontalGroup(
            l.createSequentialGroup()
                .addComponent(b1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(l.createParallelGroup()
                    .addComponent(b2, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(b3, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));

    l.setVerticalGroup(
            l.createParallelGroup()
                .addComponent(b1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(l.createSequentialGroup()
                    .addComponent(b2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(b3, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

Note that the structure is the same than the first code, only the size parameters were added.

barjak
  • 10,842
  • 3
  • 33
  • 47
  • 1
    +! for the input :-) Wish I could understand the working of a `GroupLayout`, but many a times it goes over my head. Hope your answer works for the OP :-) – nIcE cOw Feb 14 '13 at 15:20
  • 1
    @GagandeepBali: See also the contrasting examples [here](http://stackoverflow.com/a/8504753/230513) and [here](http://stackoverflow.com/a/14858272/230513). – trashgod Feb 14 '13 at 16:41
  • Thank you, grouplayout is so versatile but often goes over my head too. – Sven Feb 15 '13 at 08:49
3

I really don't know, how to use GroupLayout, though if you are willing to do the task using GridBagLayout, then this example code might can do that task for you. Please do have a look :

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

/**
 * Created with IntelliJ IDEA.
 * User: Gagandeep Bali
 * Date: 2/14/13
 * Time: 8:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class GridBagExample
{
    private GridBagConstraints gbc;
    private Random random;
    private JPanel largePanel;
    private JPanel smallPanel;
    private JPanel superSmallPanel;

    public GridBagExample()
    {
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;

        random = new Random();
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = getPanel();
        contentPane.setLayout(new GridBagLayout());

        largePanel = getPanel();
        contentPane.add(largePanel, getConstraints(
                    GridBagConstraints.BOTH, 0, 0, 1, 2, 0.7f, 1.0f));
        smallPanel = getPanel();
        contentPane.add(smallPanel, getConstraints(
                    GridBagConstraints.BOTH, 1, 0, 1, 1, 0.3f, 0.7f));
        superSmallPanel = getPanel();
        contentPane.add(superSmallPanel, getConstraints(
                    GridBagConstraints.BOTH, 1, 1, 1, 1, 0.3f, 0.3f));

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getPanel()
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(getColor());
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(getColor(), 5),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)));

        return panel;
    }

    private Color getColor()
    {
        return (new Color(
                random.nextFloat(), random.nextFloat()
                , random.nextFloat(), random.nextFloat()));
    }

    private GridBagConstraints getConstraints(
                int filler, int x, int y, int w, int h
                                 , float weightx, float weighty)
    {
        gbc.fill = filler;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
        gbc.weightx = weightx;
        gbc.weighty = weighty;

        return gbc;
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new GridBagExample().displayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143