asmodai
asmodai

Reputation: 297

Need help understanding some row constraints behaviour

In a Java project I am working on, my constraints were originally set to:

JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));

From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.")

So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on.

My problem comes when I have a JTextArea in a JScrollPane with rows set to 3:

JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");

When used with a row constraint of []10[] is only 1 row high, yet when I change

String rowConstraints = "[]10[]";

to

String rowConstraints = "[]";

it suddenly uses the full 3 rows height that I specified for the JTextArea.

Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand?

Upvotes: 0

Views: 490

Answers (1)

Piotr Sobczyk
Piotr Sobczyk

Reputation: 6583

In both cases you are using only one layout row (i.e. row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea, second column).

You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints:

String layoutConstraints = "fill,debug";

And launching application. I really recommend using this setting while working on your layouts.

Rows property that you set for JTextArea have nothing to do with layout rows. This property only tells JTextArea component what size it will try to have, as defined in javadocs:

java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...)

But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout.

Please, learn more carefully (and experiment!) what exactly JTextArea row property means.

Upvotes: 1

Related Questions