Question

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);
}
Was it helpful?

Solution

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.

OTHER TIPS

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();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top