How do I add text to a scrollable JTextPane without scrolling the JScrollPane?
Joined: Apr 12, 2012
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.