Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JScrollPane failed to update scrollbar

Posted on 2004-10-25
9
Medium Priority
?
1,260 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
Comment
Question by:csunlistek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 12407708
works fine here (using 1.4).
try updating your java runtime.
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12407818
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
ID: 12408290
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 12408307
> 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
ID: 12408427
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
ID: 12409693
Try creating your components thus:

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

Author Comment

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

Author Comment

by:csunlistek
ID: 12413122
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
ID: 12416383
(:
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

597 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