Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JScrollPane doesn't respond to client changes

Gen.JavaJun 3 2014 — edited Jun 3 2014

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

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 1 2014
Added on Jun 3 2014
4 comments
1,875 views