Hi all,

When a JTextArea is created in a JScrollPane and the contents of the JTextArea change, the JScrollPane's preferred size doesn't change.

In the following code, I create a JScrollPane and a JTextArea as a client. I then, add contents to the JTextArea and call revalidate() on it to let the JScrollPane re-update itself. However, it keeps reporting the same preferred size.

 

 

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class c
{
static JScrollPane sp;
static JTextArea ta;
public static void main (String...args)
{
JFrame f;
JButton b;
b=new JButton("ok");
b.addActionListener(new a());
ta=new JTextArea("hello");
sp=new JScrollPane(ta);
sp.setPreferredSize(new Dimension(100,70));
f=new JFrame();
f.setLayout(new FlowLayout());
f.add(sp);
f.add(b);
f.pack();
f.setVisible(true);
}
static class a implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(sp.getPreferredSize());
ta.append("this is a\nlarge piece\nof text\njust tomake\nthe height\nbigger");
ta.revalidate();
}
}
}

 

What am I doing wrong here?

Thank you

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners