JScrollPane failed to update scrollbar


I want to allow the user to enlarge the font of a JTextArea inside JScrollPane.
For some unknown reasons, the JScrollPane would not update at all
after I call jtextArea.setFont() with larger font size.
For example:
(1) The scrollbar would not show up.
(2) The enlarged jtextArea would not be visible at the end.

I have to manually resize the window to force JScrollPane
to redraw and update scrollbar.

I tried revalidate() and repaint(), but without success.
Is this a bug in JScrollPane ?
If yes, how do I go around this problem ?

Please advise.

PS. Here is my sample test code, tested under JDK 1.3.0. on Windows 2K.

package test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Panel6 extends JPanel {
  BorderLayout borderLayout1 = new BorderLayout();
  JButton jButton1 = new JButton();
  JTextArea textArea = new JTextArea();
  JScrollPane scrollPanel = new JScrollPane();

  public Panel6() {

    // button to change font
    jButton1.setText("Change Font");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {

    this.add(jButton1, BorderLayout.NORTH);
    scrollPanel.getViewport().add(textArea, null);
    this.add(scrollPanel, BorderLayout.CENTER);

    // these 2 line  below somehow cause JScrollpane to fail to update.
    // if remove, JScrollpane gets updated. why ???

  // fill in multiline text
  void fillText() {
   String text="";
   for (int i = 0; i < 40; i++) {
     text +=" ABC"+i;

  // update font style and size
  void jButton1_actionPerformed(ActionEvent e) {
    Font font1 = textArea.getFont();
    Font font2 = new Font(font1.getName(), Font.BOLD , 22 );
    // textArea.revalidate(); // this does NOT help
    // scrollPanel.getParent().repaint(); // this does NOT help

  //***** self test ********
  public static void main(String[] args) {
    // create test panel
    Panel6 t = new Panel6();
    // display panel in a frame
    JFrame frame = new JFrame();

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

works fine here (using 1.4).
try updating your java runtime.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I've also checked ur code its working fine using jdk1.4. You need to updated ur JDK.
or try to add
 line after
// scrollPanel.getParent().repaint(); // this does NOT help

Naeem Shehzad Ghuman
csunlistekAuthor Commented:
If it works in JDK 1.4 but not JDK 1.3, then I guess it is indeed a bug.

Unfortunately, I need to stay with JDK 1.3 for now.
I try this.updateUI() as Naeemg suggested but it does not work in JDK 1.3.

Any suggestion on how I can get this to work in JDK 1.3.0 ?

I guess I need to somehow notify JScrollPane about the new size and force a repaint.

Thanks for any comments.

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

> I guess I need to somehow notify JScrollPane about the new size and force a repaint.

try calling revalidate() on the scroll panes parent.
csunlistekAuthor Commented:
I added this.revalidate() after the line
 // scrollPanel.getParent().repaint(); // this does NOT help

but it does not work in JDK 1.3.

Try creating your components thus:

JTextArea textArea = new JTextArea();
 JScrollPane scrollPanel = new JScrollPane(textArea);
csunlistekAuthor Commented:
I tried the fix as  CEHJ suggested above
but it does NOT work.
csunlistekAuthor Commented:
OK. I have enough of this JDK 1.3 bug.
I am upgrading to JDK 1.4 so I can move forward.
Thanks for all the comments.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.