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(borderLayou t1);
// button to change font
jButton1.setText("Change Font");
jButton1.addActionListener (new java.awt.event.ActionListe ner() {
public void actionPerformed(ActionEven t e) {
jButton1_actionPerformed(e );
}
});
this.add(jButton1, BorderLayout.NORTH);
scrollPanel.getViewport(). add(textAr ea, 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(A ctionEvent 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().re paint(); // 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);
}
}
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(borderLayou
// button to change font
jButton1.setText("Change Font");
jButton1.addActionListener
public void actionPerformed(ActionEven
jButton1_actionPerformed(e
}
});
this.add(jButton1, BorderLayout.NORTH);
scrollPanel.getViewport().
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(
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(A
Font font1 = textArea.getFont();
Font font2 = new Font(font1.getName(), Font.BOLD , 22 );
textArea.setFont(font2);
// textArea.revalidate(); // this does NOT help
// scrollPanel.getParent().re
}
//***** 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
frame.setSize(300,200);
frame.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
try calling revalidate() on the scroll panes parent.
ASKER
I added this.revalidate() after the line
// scrollPanel.getParent().re paint(); // this does NOT help
but it does not work in JDK 1.3.
// scrollPanel.getParent().re
but it does not work in JDK 1.3.
Try creating your components thus:
JTextArea textArea = new JTextArea();
JScrollPane scrollPanel = new JScrollPane(textArea);
JTextArea textArea = new JTextArea();
JScrollPane scrollPanel = new JScrollPane(textArea);
ASKER
I tried the fix as CEHJ suggested above
but it does NOT work.
but it does NOT work.
ASKER
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.
I am upgrading to JDK 1.4 so I can move forward.
Thanks for all the comments.
(:
or try to add
this.updateUI();
line after
// scrollPanel.getParent().re
Naeem Shehzad Ghuman