Reputation: 10065
I'm using some JTextArea
in my Swing application. The surrounded JScrollPane
is added to a JPanel
using the GridBagLayout
manager.
// Pseudo Code ----
JTextArea area = new JTextArea();
area.setRows(3);
JScrollPane sp = new JScrollPane(area);
JPanel p = new JPanel(new GridBagLayout());
p.add(sp, new GridBagConstraints(
0, 0, 1, 1, 1, 0, WEST, HORIZONTAL, new Insets(4, 4, 4, 4), 0, 0));
// ----------------
Everything works fine. No problem at all until I will resize the JFrame
. Then both JTextArea
will collapse to one row. However there is enough place for at least one of them.
Why the element collapse to one row?
Does anyone know a solution? Has anyone an idea?
Upvotes: 1
Views: 2363
Reputation: 3739
I had a similar problem recently where I had two JTextAreas with different GridBagConstraint.weighty values and both were set to GridBagConstraint.fill = BOTH. When I would change the text in the JTextAreas, they would change sizes (which I would expect not to happen if weights and fill values are set). Oddly enough, all I had to do was set a preferred size to the JScrollPanes that surrounded the JTexAreas. The preferred size dimension could be anything, as the "fill = BOTH" caused the preferred size to be ignored, but it did something magical to fix my problem.
Upvotes: 0
Reputation: 10065
If I use a BorderLayout
around my elements, it works.
Pete, MrWiggles, Thank you for your help!!
Upvotes: 0
Reputation: 18075
Also make sure you are setting the 'preferred size' property on your scrollpane's. I've had strange behavior (panes/fields collapsing/disappearing) when frames and panels get resized when this property is not set.
Upvotes: 2
Reputation: 21184
I believe this is because you have your weighty set to 0 (6th argument to the GridBagConstraints constructor). You'll need to increase this if you want your component to grow vertically.
Upvotes: 2