• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1262
  • Last Modified:

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();

1 Solution
works fine here (using 1.4).
try updating your java runtime.
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.

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

> 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.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now