Solved

JScrollPane failed to update scrollbar

Posted on 2004-10-25
1,201 Views
Last Modified: 2008-03-17
Hi,

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() {
    this.setLayout(borderLayout1);

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

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

    fillText();
    // these 2 line  below somehow cause JScrollpane to fail to update.
    // if remove, JScrollpane gets updated. why ???
     textArea.setWrapStyleWord(true);
     textArea.setLineWrap(true);
  }

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

  // update font style and size
  void jButton1_actionPerformed(ActionEvent e) {
    Font font1 = textArea.getFont();
    Font font2 = new Font(font1.getName(), Font.BOLD , 22 );
    textArea.setFont(font2);
    // 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();
    frame.getContentPane().add(t);
    frame.setSize(300,200);
    frame.setVisible(true);
  }

}
0
Question by:csunlistek
    9 Comments
     
    LVL 92

    Accepted Solution

    by:
    works fine here (using 1.4).
    try updating your java runtime.
    0
     
    LVL 5

    Expert Comment

    by:Naeemg
    I've also checked ur code its working fine using jdk1.4. You need to updated ur JDK.
    or try to add
        this.updateUI();
     line after
    // scrollPanel.getParent().repaint(); // this does NOT help


    Naeem Shehzad Ghuman
    0
     

    Author Comment

    by:csunlistek
    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.

    0
     
    LVL 92

    Expert Comment

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

    try calling revalidate() on the scroll panes parent.
    0
     

    Author Comment

    by:csunlistek
    I added this.revalidate() after the line
     // scrollPanel.getParent().repaint(); // this does NOT help

    but it does not work in JDK 1.3.


    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Try creating your components thus:

    JTextArea textArea = new JTextArea();
     JScrollPane scrollPanel = new JScrollPane(textArea);
    0
     

    Author Comment

    by:csunlistek
    I tried the fix as  CEHJ suggested above
    but it does NOT work.
    0
     

    Author Comment

    by:csunlistek
    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.
    0
     
    LVL 92

    Expert Comment

    by:objects
    (:
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now