Question

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:

enter image description here enter image description here

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: enter image description here

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.

Was it helpful?

Solution

  1. Save yourself some sanity points and enable debug borders on the MiGLayout: new MigLayout("debug").
  2. It's possible that the gap is indeed pushing the components apart, but the layout isn't using all of the available space. This should be obvious if you turn on debug borders. Try making the layout use all available space: new MigLayout("debug, fill").
  3. Instead of placing a gap between your label and the cell edge, you may want to place a gap between the two columns: new MigLayout("debug, fill", "[]rel:push[]"). The difference should become clear when using a multi-row layout. In this case, you may not need the fill constraint: new MigLayout("debug", "[]rel:push[]").

I don't have a compiler handy to run this against, but I hope I've given you something to work with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top