Link to home
Start Free TrialLog in
Avatar of csunlistek
csunlistek

asked on

JScrollPane failed to update scrollbar

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

}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naeemg
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
Avatar of csunlistek

ASKER

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.

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

try calling revalidate() on the scroll panes parent.
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);
I tried the fix as  CEHJ suggested above
but it does NOT work.
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.