Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Scrolling in JTextPane - not the usual question

Thread view: 
ni4ni - 01 Feb 2005 11:22 GMT
I have a JTextPane inside a JScrollPane .
I add a bulk of text to it and highlight few scattered words.

Until now everything works just fine.
Now I have a forward and backward buttons which should scroll the text
to the next or previous highlighted word respectively.

I do not need to scroll to the last line as most people post here, but
to a certain point within the text (character number or maybe a line
number).

Any ideas how to do that ?
Roland - 01 Feb 2005 12:03 GMT
> I have a JTextPane inside a JScrollPane .
> I add a bulk of text to it and highlight few scattered words.
[quoted text clipped - 8 lines]
>
> Any ideas how to do that ?

int pos = ... // calculate position of word in document
Rectangle r = yourTextPane.modelToView(pos);
Rectangle vis = yourTextPane.getVisibleRect();
r.height = vis.height;
yourTextPane.scrollRectToVisible(r);

Untested, but based on JEditorPane.scrollToReference(String)
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

klynn47@comcast.net - 01 Feb 2005 12:48 GMT
What about setCaretPosition? I needed to move the cursor to the end of
the text that I entered, and I used setCaretPosition
Roland - 01 Feb 2005 12:59 GMT
> What about setCaretPosition? I needed to move the cursor to the end of
> the text that I entered, and I used setCaretPosition

Yeah, that seems much simpler. I don't know if takes care of scrolling
when the new caret position isn't yet visible. Just a case of trying it
out, I guess.
And setCaretPosition also changes the position of the input caret, but
if OP doesn't mind, he/she should use this.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

ni4ni - 01 Feb 2005 15:26 GMT
setCaretPosition did not work for me (I tried it before posting my
question).

Anyway, I ended up using
myJScrollPane.getVerticalScrollBar().setValue() after I realized that
scrolling to the right line is good enough for me.

Thanks

Guy


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.