2

Consider the following figure:

Required Design

I need to develop a swing GUI the looks like this. I simply named them jLabel's but there a few images and jLabels in it. The default awt background visible is a JPanel and each red background visible is a serperate JPanel. Now I need them to get stacked as shown above. I tried a number of LayoutManagers and still it doesn't work.

The important point here is that the number of red colored divs are not constant. If there is only one red colored div then it must be displayed at the top, not at the center. As far as i know GridBagLayout should work, but it centers the single red colored jpanel available. All the layout managers are centering them but not stacking them from top to bottom.

1
  • I quite didn't get what you meant.The contents of the JPanel are truly dynamic. What is the APPROPRIATE layout manager that does this?? and to top it off the outermost jpanel(the one with awt background) is added to a JScrollPane. So FlowLayout doesn't work
    – sasidhar
    Mar 12, 2012 at 21:16

5 Answers 5

3

Even with anchor set to NORTH then the panels will still be centered. You could work around it by adding a dummy panel to fill the remaining space. Personally I'd stay well away from GridBagLayout though.

JFrame frame = new JFrame();
JPanel content = new JPanel();
content.setBorder(BorderFactory.createLineBorder(Color.red));
frame.setContentPane(content);
frame.getContentPane().setLayout(new GridBagLayout());
frame.setSize(400, 300);

for (int i = 0; i < 3; i++) {
    JPanel panel = new JPanel();
    panel.add(new JLabel("label1"));
    panel.add(new JLabel("label2"));
    panel.add(new JLabel("label3"));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    GridBagConstraints con = new GridBagConstraints();
    con.gridy = i;
    con.gridx = 0;
    con.anchor = GridBagConstraints.NORTHWEST;
    con.ipady = 10;
    frame.getContentPane().add(panel, con);
}

// dummy panel to use up the space (force others to top)
frame.getContentPane().add(
        new JPanel(),
        new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.NORTHWEST,
                GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0,
                0));

frame.setVisible(true);

GroupLayout example (my favourite layout manager).

JFrame frame = new JFrame();
JPanel content = new JPanel();
frame.setContentPane(content);
frame.getContentPane().setLayout(
        new BoxLayout(content, BoxLayout.Y_AXIS));
frame.setSize(400, 300);
GroupLayout gLayout = new GroupLayout(content);
content.setLayout(gLayout);
ParallelGroup hGroup = gLayout.createParallelGroup();
gLayout.setHorizontalGroup(hGroup);
SequentialGroup vGroup = gLayout.createSequentialGroup();
gLayout.setVerticalGroup(vGroup);
for (int i = 0; i < 3; i++) {
    JPanel panel = new JPanel();
    panel.add(new JLabel("label1"));
    panel.add(new JLabel("label2"));
    panel.add(new JLabel("label3"));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    hGroup.addComponent(panel);
    vGroup.addComponent(panel, GroupLayout.PREFERRED_SIZE,
            GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
    vGroup.addGap(10);
}

frame.setVisible(true);
2
  • Is group Layout meant to be for hand coding code??? I stayed away from it all my life... And i didn't understand where you inserted this dummy panel?
    – sasidhar
    Mar 12, 2012 at 21:34
  • 1
    @sasidhar Yes GroupLayout can be used "by hand" - having said that a lot of people find it hard to understand. The dummy panel is the one added after the 3 example panels - I've added a comment to the code to point it out.
    – Adam
    Mar 12, 2012 at 21:53
2

you can use Vertical BoxLayout, for example: http://www.java-tips.org/java-se-tips/javax.swing/how-to-use-swing-boxlayout.html

4
  • 1
    tried, this does get the first element in the intended position, but when there are only two elements, the next element is expanded to occupy the total remaning place
    – sasidhar
    Mar 12, 2012 at 21:27
  • Try adding the box layout panel to the north of a panel with a border layout. Mar 13, 2012 at 10:54
  • Had the same issue, solved using BoxLayout with Axis=y. thanks Wajdy Jun 7, 2012 at 5:25
  • This answer is very unsatisfactory. I couldn't reproduce the image posted by OP using BoxLayout. Feb 26, 2015 at 19:10
2

nobody tell us that all JComponents must be visible, for example

enter image description here

enter image description here

enter image description here

from code

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

public class AddComponentsAtRuntime {

    private JFrame f;
    private JPanel panel;
    private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;

    public AddComponentsAtRuntime() {
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel(new GridLayout(0, 1));
        f.add(panel, "Center");
        f.add(getCheckBoxPanel(), "South");
        f.setLocation(200, 200);
        f.pack();
        f.setVisible(true);
    }

    private JPanel getCheckBoxPanel() {
        checkValidate = new JCheckBox("validate");
        checkValidate.setSelected(false);
        checkReValidate = new JCheckBox("revalidate");
        checkReValidate.setSelected(false);
        checkRepaint = new JCheckBox("repaint");
        checkRepaint.setSelected(false);
        checkPack = new JCheckBox("pack");
        checkPack.setSelected(false);
        JButton addComp = new JButton("Add New One");
        addComp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel b = new JPanel(new GridLayout(0, 4));
                b.setBackground(Color.red);
                b.setBorder(new LineBorder(Color.black, 2));
                //b.setPreferredSize(new Dimension(600, 20));
                for (int i = 0; i < 4; i++) {
                    JLabel l = new JLabel("label" + i + 1);
                    b.add(l);
                    if (i == 2) {
                        l.setVisible(false);
                    }
                }
                panel.add(b);
                makeChange();
                System.out.println(" Components Count after Adds :" + panel.getComponentCount());
            }
        });
        JButton removeComp = new JButton("Remove One");
        removeComp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int count = panel.getComponentCount();
                if (count > 0) {
                    panel.remove(0);
                }
                makeChange();
                System.out.println(" Components Count after Removes :" + panel.getComponentCount());
            }
        });
        JPanel panel2 = new JPanel();
        panel2.add(checkValidate);
        panel2.add(checkReValidate);
        panel2.add(checkRepaint);
        panel2.add(checkPack);
        checkPack.setSelected(true);
        panel2.add(addComp);
        panel2.add(removeComp);
        return panel2;
    }

    private void makeChange() {
        if (checkValidate.isSelected()) {
            panel.validate();
        }
        if (checkReValidate.isSelected()) {
            panel.revalidate();
        }
        if (checkRepaint.isSelected()) {
            panel.repaint();
        }
        if (checkPack.isSelected()) {
            f.pack();
        }
    }

    public static void main(String[] args) {
        AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
    }
}
2
  • I think you missed a point here, the image i gave you clearly indicates that the height of the background JPanel is fixed and when it increases beyond that point the JScrollPane it is attached to will create scrollbars. using this.pack an creating a JPanel just enough for the components is a trivial solution that doesn't suit my scenario.
    – sasidhar
    Mar 13, 2012 at 4:11
  • @sasidhar all things around are allways simpler that you thinking about,
    – mKorbel
    Mar 13, 2012 at 7:45
1

You should try the MigLayout it is simple yet powerful. Below I tell miglayout to grow elements, and fill all possible space, then after each element I tell it to go to a new line (wrap). You can find examples and tutorial on MigLayout page http://www.miglayout.com/:

import net.miginfocom.swing.MigLayout;

public class PanelLearning extends JPanel {

public PanelLearning() {
    setLayout(new MigLayout("", "[grow, fill]", ""));

    for (int i = 0; i < 3; i++) {
        JPanel panel = new JPanel();
        panel.add(new JLabel("label1"));
        panel.add(new JLabel("label2"));
        panel.add(new JLabel("label3"));
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        add(panel, "span, wrap");
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Login");
    frame.setVisible(true);

    frame.setContentPane(new PanelLearning());

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

Make sure GridBagConstraints.anchor = GridBagConstraints.NORTH when you add components to the panel.

2
  • tried GridBagConstraints.NORTH,GridBagConstraints.FIRST_LINE_START and also GridBagConstraints.PAGE_START nothing seems to work
    – sasidhar
    Mar 12, 2012 at 21:26
  • @sasidhar Yes I tried NORTH but it didn't work, I had to add a dummy panel to fill the remaining space to force stuff to the top. See my solution.
    – Adam
    Mar 12, 2012 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.