Frage

I'm trying to make a neat centred for structure but the labels and fields o not have an equal space between them, I'm thinking I may have to use two columns instead of merging them.

This is what I want to achieve:

+-----------------------------------------------+
|                                               |
|            lbl1 [    txt1     ]               |
|            lbl2 [    txt2     ]               |
|            lbl3 [    txt3     ]               |
|            lbl4 [    txt4     ]               |
|                 [btn5][btn6]                  |
|                                               |
|                                               |
|                                               |
|                                               |
+-----------------------------------------------+

This is my current code:

contentPane.setLayout(new MigLayout("fill","center"));    
contentPane.add(lbl1, "split 2, span");
contentPane.add(txt1, "wrap");
contentPane.add(lbl2, "split 2, span");
contentPane.add(txt2, "wrap");
contentPane.add(lbl3, "split 2, span");
contentPane.add(txt3, "wrap");
contentPane.add(lbl4, "split 2, span");
contentPane.add(txt4, "wrap");
contentPane.add(btn5, "split 2, span");
contentPane.add(btn6);

This is how the issue looks visually, this is the spaces between the labels and text fields.

out of line

How can I achieve this layout using MigLayout?

War es hilfreich?

Lösung

Problem is in "split 2" atribute. By passing that attribute you are splitting cells where JLabels are. So one JLabel and one JTextField fits in one cell. Since JLabels have different sizes (during different length of text) you have that effect of nonequal space.

This should solve problem:

contentPane.setLayout(new MigLayout("center"));
contentPane.add(lbl1, "alignx trailing");
contentPane.add(txt1, " wrap");
contentPane.add(lbl2, "alignx trailing");
contentPane.add(txt2, "wrap");
contentPane.add(lbl3, "alignx trailing");
contentPane.add(txt3, " wrap");
contentPane.add(lbl4, "alignx trailing");
contentPane.add(txt4, " wrap");
contentPane.add(btn5, "skip, split2, growx");
contentPane.add(btn6,"growx");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top