MigLayout: unexpected layout near component

Playing a bit to demonstrate how easy it is to fulfill a mock requirement using MigLayout, I was surprised by the result:

MigLayout layout = new MigLayout("wrap 3, debug"); JComponent content = new JPanel(layout); content.add(new JLabel("First:")); content.add(new JScrollPane(new JTextArea(10, 20)), "skip, spany"); content.add(new JLabel("Second")); content.add(new JTextField(10)); content.add(new JLabel("third")); content.add(new JTextField(10)); //content.add(new JLabel()); 

The layout idea is quite simple:

  • three columns
  • last column covering all rows
  • first two columns - group of shortcut / component pairs

Unexpectedly, the last row of the first two columns occupies the entire available vertical space, which leads to the positioning of the last pair in its middle (the upper alignment is not an option, since they must be aligned along the baseline with each other)

enter image description here

uncommenting the last line above (adding a practically invisible dummy) shows the expected layout, but a hack that should not be part of the production code

enter image description here

Question: how to achieve the expected layout without hacking?

+4
source share
1 answer

may be a mistake :

a less hacky way to work around (applicable if the number of lines is known at the time the form was created) is to explicitly define line restrictions

 MigLayout layout = new MigLayout("wrap 3, debug", "", "[][][][]"); 

which defines one row more than is actually necessary for components on the side of the enclosing component

+4
source

All Articles