aspose file tools
The moose likes Swing / AWT / SWT and the fly likes How do I add text to a scrollable JTextPane without scrolling the JScrollPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "How do I add text to a scrollable JTextPane without scrolling the JScrollPane?" Watch "How do I add text to a scrollable JTextPane without scrolling the JScrollPane?" New topic
Author

How do I add text to a scrollable JTextPane without scrolling the JScrollPane?

Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
I am using a JTextPane within a JScrollPane to display more text than the area provides. The controls live on a custom JDialog box and everything is fully initialized in the dialog's constructor. The scroll bars work and allow me to scroll around and view all of the text, but the problem is that the scrollbar is initialized at the lowest position. In other words, when my dialog appears the text field is scrolled down and displays the bottom of the block of text instead of the top.

The first thing I tried to do was set the vertical scrollbar's value to 0 after setting up the dialog, but the request is ignored. I thought this might be the result of the dialog being resized with "pack()" so I called this after pack() and it is still ignored.
As far as I can tell something happens after the dialog's constructor that changes this setting and wipes out mind, but I don't know what it is.




I rigged up the code to perform this command in one of my event handlers so that I could test it completely outside the initialization process and it worked fine, although if I check the current value I see that it's 176 instead of 0. That proves to me that something outside of my doing has moved it. Also, I have found that the size of the scrollpane changes from (410, 120) within the dialog's constructor to (420, 130). I thought this might be due to the appearance of the scrollbars but it is not. I changed the flags from the ..._SCROLLBAR_AS_NEEDED to the ..._SCROLLBAR_ALWAYS flags and the values continued to be as I see them. The min and max values always remain the same with min being 307 and max being 0.




At this point I am thinking that the only way to reposition my scrollbar to the top is to rig up some InvokeLater request, but I'd rather not have my text field appear to "bounce" after initialization.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4167
    
    3

Rob Camick has a blog post about that. If you want more flexibility, you can find my additions to Rob's design here.


luck, db
There are no new questions, but there may be new answers.
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Darryl Burke wrote:Rob Camick has a blog post about that. If you want more flexibility, you can find my additions to Rob's design here.

Nice link! It answered my question and then it answered it again with another method that felt more secure!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I add text to a scrollable JTextPane without scrolling the JScrollPane?
 
Similar Threads
add a JTextPane to a JScrollPane
Scrolling a JTextPane within a JScrollPane
Set scrollbar to top
JTextField in JScrollPane
JScrollPane.getViewport().setScrollPosition()