I am using MigLayout and I would like to create an effect like using BoxLayout's "glue" to create an invisible space that will flexibly fill the area between two components. From the tutorial:
I understand that in MigLayout, gaps are used to create spaces between components. I can create a rigid space with "gap 10"
. The MiG Layout Cheatsheet says that I can append push
to a bound size with a gap to "make that gap "greedy" and take any left over space" (e.g. "gap rel:push"
), but that doesn't seem to work in the following code that I've tried:
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new MigLayout());
JLabel label1 = new JLabel("Testing");
label1.setBorder(BorderFactory.createLineBorder(Color.black)); //Border to clearly show JLabel boundaries
panel.add(label1, "");
JLabel label2 = new JLabel("Testing Once Again");
label2.setBorder(BorderFactory.createLineBorder(Color.black)); //Border to clearly show JLabel boundaries
panel.add(label2, "gap rel:push");
frame.setContentPane(panel);
frame.pack();
frame.setMinimumSize(new Dimension(400, 100));
frame.setPreferredSize(new Dimension(400, 100));
frame.setVisible(true);
Which displays a window like this:
I would expect for the "gap rel:push"
parameter to work like horizontal glue in the picture above and thus push the JLabel on the right all the way to the right edge of the window, but it doesn't.
How can I get the same effect as BoxLayout's glue using MigLayout? Am I misusing the "gap"
parameter or is there some other way to accomplish it?
Also, I realize that I could use docking in the example given, but I'd like an answer without that because it won't work in a more complex layout I'm creating.