How to disable automatic scrolling of JScrollPane / JTextPane (Swing)  
Author Message






PostPosted: 2005-6-22 3:45:00 Top

java-programmer, How to disable automatic scrolling of JScrollPane / JTextPane (Swing) Hi,

I want to be able to enable / disable the automatic scrolling performed when
adding text to a JTextPane. A JScrollPane has a viewport set to the
textpane. The text is added within a thread invoked using
SwingUtilities.invokeLater(...). Only the automatic scrolling should be
disabled, the user must still be able to scroll using the scrollbars etc.
and be able to enable the automatic scrolling again. (I'm using
jdk1.5.0_03.)

Here is how some parts my code look like:

...
<init>
...
jTextPane = new JTextPane();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(textPane);
...


<some-method>
...
StyledDocument styledDocument = (StyledDocument)jTextPane.getDocument();
...
<call addText-method>
...


<addText-method>
...
Runnable r = new Runnable() {
public void run() {
try {
styledDocument.insertString(styledDocument.getLength(), text,
style);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
...

Every time the <addText-method> is called the JScrollPane scrolls to the end
of the document. How can I enable / disable that automatic scrolling and
still use a thread / invokeLater to add the text?


Greetings,

ODB



 
margielm





PostPosted: 2005-6-22 16:03:00 Top

java-programmer >> How to disable automatic scrolling of JScrollPane / JTextPane (Swing) here is a solution for your problem:

JTexyPane jTextPane1 = new JTextPane();
jTextPane1.setAutoscrolls(false);

ODB wrote:
> Hi,
>
> I want to be able to enable / disable the automatic scrolling performed when
> adding text to a JTextPane. A JScrollPane has a viewport set to the
> textpane. The text is added within a thread invoked using
> SwingUtilities.invokeLater(...). Only the automatic scrolling should be
> disabled, the user must still be able to scroll using the scrollbars etc.
> and be able to enable the automatic scrolling again. (I'm using
> jdk1.5.0_03.)
>
> Here is how some parts my code look like:
>
> ...
> <init>
> ...
> jTextPane = new JTextPane();
> JScrollPane jScrollPane = new JScrollPane();
> jScrollPane.setViewportView(textPane);
> ...
>
>
> <some-method>
> ...
> StyledDocument styledDocument = (StyledDocument)jTextPane.getDocument();
> ...
> <call addText-method>
> ...
>
>
> <addText-method>
> ...
> Runnable r = new Runnable() {
> public void run() {
> try {
> styledDocument.insertString(styledDocument.getLength(), text,
> style);
> } catch (BadLocationException e) {
> e.printStackTrace();
> }
> }
> };
> SwingUtilities.invokeLater(r);
> ...
>
> Every time the <addText-method> is called the JScrollPane scrolls to the end
> of the document. How can I enable / disable that automatic scrolling and
> still use a thread / invokeLater to add the text?
>
>
> Greetings,
>
> ODB

 






PostPosted: 2005-6-22 19:34:00 Top

java-programmer >> How to disable automatic scrolling of JScrollPane / JTextPane (Swing)
"margielm" <email***@***.com> wrote in message
news:email***@***.com...
> here is a solution for your problem:
>
> JTexyPane jTextPane1 = new JTextPane();
> jTextPane1.setAutoscrolls(false);
>

Hmmm... Do you have any idea what my problem is and what the setAutoscrolls
method does??? The setAutoscrolls method was my first guess, but if you look
at the specs you'll see that this method does something else.


ODB