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

