tags:

views:

794

answers:

4

Hi.

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?

Thank you in advance!
Best Regards
Zubi

+1  A: 

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.

MrWiggles
+2  A: 

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.

Pete
A: 

If I use a BorderLayout around my elements, it works.
Pete, MrWiggles, Thank you for your help!!

Zubi
A: 

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.

Ross