JPanel with JTextArea in JScrollPane grows on resize, but does not shrink

Hi

Maybe someone here can help me with my layout problem, which kept me busy the last days. I have a variable number of JTextAreas that I want to layout vertically, each taking up the full available width. As the totalized height of all text areas in my app will certainly be larger than the available height, I put them all in a JScrollPane.

The problem: If I widen the frame, the width of the text areas grows accordingly, but if I narrow it afterwards, the width of the text areas does not change, resulting in a horizontal scroll bar. What can I do about it?

In the following code, the frame "foo" works the way I want (but "foo" is not simply extendable to work with more than one text area) and "bar" shows the unwanted behavior.

import java.awt.*;

import javax.swing.*;

publicclass ScrollableShrink{

publicstaticvoid main(String[] args){

foo();

bar();

}

publicstaticvoid foo(){

JFrame f =new JFrame("foo");

Container c = f.getContentPane();

JTextArea jta =new JTextArea(someText);

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

JScrollPane jsp =new JScrollPane(jta);

c.add(jsp, BorderLayout.CENTER);

f.setSize(100,100);

f.setVisible(true);

}

publicstaticvoid bar(){

JFrame f =new JFrame("bar");

Container c = f.getContentPane();

JPanel p =new JPanel(new GridLayout(0, 1));

JTextArea jta =new JTextArea(someText);

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

p.add(jta);

jta =new JTextArea(someText);

jta.setBackground(Color.GREEN);

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

p.add(jta);

JScrollPane jsp =new JScrollPane(p);

c.add(jsp, BorderLayout.CENTER);

f.setSize(100,100);

f.setVisible(true);

}

privatestatic String someText ="Lorem ipsum dolor sit amet, consectetur " +

"adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore " +

"magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +

"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " +

"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +

"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia " +

"deserunt mollit anim id est laborum.";

}

Any help would be greatly appreciated.

ToadyFrog

[3996 byte] By [ToadyFroga] at [2007-11-27 8:47:57]
# 1

I think you can use the scrollable panel from this example:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=780453

The key method is the getScrollableTracksViewportWidth(..) which has been overriden to return true. I believe says to make the width of the panel the same as the width of the viewport.

camickra at 2007-7-12 20:54:01 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks camickr,

The width problem is solved now, but why do all components in that ScrollablePanel now get the same height (which is the maximum preferred height of all the components)?

class ScrollablePanel extends JPanel implements Scrollable {

public ScrollablePanel() { super(); }

public ScrollablePanel(LayoutManager l) { super(l); }

public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); }

public int getScrollableUnitIncrement(Rectangle vr, int o, int d) { return 20; }

public int getScrollableBlockIncrement(Rectangle vr, int o, int d) { return 60; }

public boolean getScrollableTracksViewportWidth() { return true; }

public boolean getScrollableTracksViewportHeight() { return false; }

}

ToadyFroga at 2007-7-12 20:54:01 > top of Java-index,Desktop,Core GUI APIs...
# 3
Because you are using a GridLayout, which makes all components added to the grid the same sime. You can try using a vertical BoxLayout instead.
camickra at 2007-7-12 20:54:01 > top of Java-index,Desktop,Core GUI APIs...
# 4
Great horny toads! It's working! Thanks, camickr, you saved my week.
ToadyFroga at 2007-7-12 20:54:01 > top of Java-index,Desktop,Core GUI APIs...