Skip to main content

JEditorPane scroll to top

4 replies [Last post]
drayah
Offline
Joined: 2005-09-01

Hi is there an easy way to scroll a JEditorPane that contains some text to the top programatically?

In my program i call setPreferredSize() on a JEditorPane to set its size and subsequently add a load of text to it using its setText() method. Then I add this JEditorPane to a JScrollPane. When I run the program I see that the JScrollpane has scrolled all the way to the bottom, but I want to display my text from the beginning.

Calling scrollRectToVisible(new Rectangle(1,1,1,1)) does nothing, anybody out there who can help?

heres my code:
-----
JEditorPane edit = new JEditorPane();
edit.setPreferredSize(new Dimension(325, 120));
edit.setText(messages);
JScrollPane scroller = new JScrollPane(edit);
edit.scrollRectToVisible(new Rectangle(1,1,1,1));

-------
-gio

Message was edited by: drayah

Message was edited by: drayah

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hlander123
Offline
Joined: 2005-05-12

Same as before, but with better formatting.

When I run the following code the scroll pane appears scrolled to the top -- even without the scrollRectToVisible method call. I am running JRE 1.5.0_03.

[PRE]
public class Test
{
public static void main(String theParams[])
{
JEditorPane edit = new JEditorPane();
String theText =
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"THE END";

edit.setPreferredSize(new Dimension(325, 120));
edit.setText(theText);
JScrollPane scroller = new JScrollPane(edit);
// edit.scrollRectToVisible(new Rectangle(1,1,1,1));

JDialog dialog = new JDialog();
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);

}
}

[/PRE]

leouser
Offline
Joined: 2005-12-12

Hi!

have you folks tried setting the caret position to 0? This sounds like essentially the same problem Ive had before. Lots of text, scrolled to the bottom... need to be at the top.

setCaretPosition( 0 );

that did the trick for me, suddenly I was at the top of the page, one simple method call.

leouser :)

drayah
Offline
Joined: 2005-09-01

Ok I found the solution to my problem on another forum

My problem is solved with the following changes to the code

code:
-----
final JEditorPane edit = new JEditorPane();
edit.setPreferredSize(new Dimension(325, 120));
edit.setText(messages);
JScrollPane scroller = new JScrollPane(edit);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
edit.scrollRectToVisible(new Rectangle(1,1,1,1));
}
});
----

Can anybody clue me in as to why I have to invoke a new Runnable for later execution on the event dispatch thread?

Glad the problem is solved but I dont understand why

regards,

gio

hlander123
Offline
Joined: 2005-05-12

When I run the following code the scroll pane appears scrolled to the top -- even without the scrollRectToVisible method call. I am running JRE 1.5.0_03.
------------------
[CODE]
public class Test
{
public static void main(String theParams[])
{
JEditorPane edit = new JEditorPane();
String theText =
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"jhskdjh jksdh fksjdhfskdjfhksd jfh skdjf kdjshks djhfksdjfsdf sdfsdfjsdhfkjsdhfksjdhfksdjfhskdjf hskjfksdjfh dk" +
"THE END";

edit.setPreferredSize(new Dimension(325, 120));
edit.setText(theText);
JScrollPane scroller = new JScrollPane(edit);
// edit.scrollRectToVisible(new Rectangle(1,1,1,1));

JDialog dialog = new JDialog();
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);

}
}
[/CODE]