I am struggling with MIGLayout
- I generally know its basics, but today encountered a problem.
To simplify the situation:
I want to have three rows, such that in the first and third row there is a non-fixed amount of objects (e.x. JTextField
components). Width of first text field from first row should be the same as width of first text field from the third row and so on.
The last text field in row, which contains the most of them, should grow for the rest of frame size.
These two rows should be distinguished by JSeparator
, which also should be of length of frame.
I have already seen How to draw a separator across a panel using MigLayout, but since I need the fields to be aligned exactly one below another, it doesn't work for me.
This is my code:
public class Main extends JFrame {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Main();
}
public Main() {
setLayout(new MigLayout(new LC()));
setPreferredSize(new Dimension(800, 300));
CC cc = new CC().growX();
CC wcc = new CC().growX().wrap();
add(new JTextField("Area 1"), cc);
add(new JTextField("Area 2"), cc);
add(new JTextField("Area 3"), wcc);
add(new JSeparator(), new CC().spanX().pushX().growX().wrap());
add(new JTextField("Longer Area 1"), cc);
add(new JTextField("Longer Area 2"), cc);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
This is how it looks like:
and this is how I want it to looked like:
What is the best solution to this problem? Amount of text fields will be changing dynamically. Thanks in advance.
Use a layout constraint called
fill
and make everythinggrowx
. Then addspanx
to the last component on each row.The reason your example looked strange was because you were using
pushx
overfill
.The following example produces this output.
Runnable Example