5

I'd love to get a demonstration of how to make this glue thing work; I've been trying to get it to work and nothing happens...

A good example would be implementation of the class CenteringPanel: all it does is get a JComponent and centers it, leaving it unstretched on the center of a window... i tried coding something like that:

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;


public class CenteringPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    public CenteringPanel(JComponent toCenter) {
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        add(Box.createHorizontalGlue());
        add(Box.createVerticalGlue());
        add(toCenter);
        add(Box.createVerticalGlue());
        add(Box.createHorizontalGlue());
    }

}
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • Why not use a BorderLayout and just place the component in the center? – LazyCubicleMonkey Jan 01 '12 at 00:41
  • it stretchs it, i dont want the content to be stretched – Ofek Ron Jan 01 '12 at 00:43
  • Does it make sense to use horizontal glue when the `BoxLayout` is vertical? – sarnold Jan 01 '12 at 00:46
  • i dont know, i am new to GUI and am trying to learn. been reading tutorials like crazy and yet havent got to understand how glue works – Ofek Ron Jan 01 '12 at 00:48
  • Not sure, but I believe glue separates components. So if you add a component above and below the vertical glue with a preferred size, that might do the trick. – LazyCubicleMonkey Jan 01 '12 at 00:51
  • @Ofek Ron: You may want to check my answer to this other question: http://stackoverflow.com/questions/2709220/how-do-i-keep-jtextfields-in-a-java-swing-boxlayout-from-expanding/14630039#14630039 – damix911 Jan 31 '13 at 16:08

1 Answers1

5

If your goal is to center a component, then a GridBagLayout will do the job nicely:

public class CenteringPanel extends JPanel {
    public CenteringPanel(JComponent child) {
        GridBagLayout gbl = new GridBagLayout();
        setLayout(gbl);
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        gbl.setConstraints(child, c);
        add(child);
    }
}

GridBagLayout will create a single cell that fills the panel. The default value for the constraints are to center each component within its cell both horizontally and vertically and to fill in neither direction.

If your goal is to use Glue in a BoxLayout to center a component, then the job is a bit more complex. Adding horizontal glue with a vertical BoxLayout doesn't help, because the components are stacked vertically (and similarly for a horizontal BoxLayout). Instead you need to restrict the size of the child and use its alignment. I didn't try it, but for a vertical BoxLayout, something like this should work:

public class CenteringPanel {
    public CenteringPanel(JComponent child) {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        GridBagConstraints c = new GridBagConstraints();
        child.setMaximumSize(child.getPreferredSize());
        child.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(Box.createVerticalGlue());
        add(child);
        add(Box.createVerticalGlue());
    }
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521