scrollRectToVisible() doesn't work?
I have a JTextPane inside a JScrollPane. This text pane is displaying an HTML document. I've implemented a search function that gives me an offset in the string backing the text pane. I'd like to scroll to this offset. This is what I'm currently doing...
Rectangle r = txtPane.modelToView(myIntOffset);
txtPane.scrollRectToVisible(r);
This always scrolls to the very bottom of the document, even if I hard code what is passed to scrollRectToVisible. Any ideas?
[488 byte] By [
J.D.a] at [2007-11-27 9:03:35]

# 1
You probably have some other code that is causing it to scroll to the bottom.
If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
see http://homepage1.nifty.com/algafield/sscce.html,
that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the "Code Formatting Tags",
see http://forum.java.sun.com/help.jspa?sec=formatting,
so the posted code retains its original formatting.
# 2
Anyone else? I find camickr to not be very helpful in any thread he participates in.... And, I read alot on these forums...
J.D.a at 2007-7-12 21:35:48 >

# 3
> And, I read alot on these forums
Then you should know that the majority of time, things dont' work because of a programmer mistake. We are not mind readers. We don't know what the rest of your program is doing based on 2 lines of code.
> I find camickr to not be very helpful in any thread he participates in....
And I guess I made the mistake of helping you in these postings:
http://forum.java.sun.com/thread.jspa?threadID=5186169
http://forum.java.sun.com/thread.jspa?threadID=5141986
I won't make that mistake again.
# 4
> Anyone else? I find camickr to not be very helpful
> in any thread he participates in.... And, I read
> alot on these forums...
That's strange, when it comes to Swing camickr is one of the most knowledgeable posters here.
And in this case I'd agree with him, there probably is some other code affecting the outcome. Post a short program demonstrating the weird behavior and I'm sure he'll be the first to spot what's wrong with it (that is if he's still willing to help after your comment).
dwga at 2007-7-12 21:35:48 >

# 5
@camickr
>And I guess I made the mistake of helping you in these postings:
>http://forum.java.sun.com/thread.jspa?threadID=5186169
>http://forum.java.sun.com/thread.jspa?threadID=5141986
>I won't make that mistake again.
The suggestions are nice, but most often they've led me to dead ends. I was hoping to get some other suggestions as why scrollRectToVisible() is off. I guess the prevailing suggestion here is that it's being scrolled somewhere else after I use this method. I'll step through my code once again to make sure this isn't the case, which I'm 99% sure of.
Maybe someone has had a case where this has happened and the problem wasn't directly related to a scroll in another area of code. Maybe there are some other, non-obvious conditions in which this might occur. I'm really looking for those, because like I said, I'm fairly certain I've ruled out the obvious.
J.D.a at 2007-7-12 21:35:48 >

# 6
Here's what I've tried, but doesn't seem to work, i.e. the viewport doesn't scroll anywhere, just stays at the bottom.
Anyone have a solution to this?JFrame frame = new JFrame("Scrolling");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JTextArea area = new JTextArea(5, 5);
JScrollPane scroller = new JScrollPane(area);
panel.add(scroller);
frame.add(panel);
frame.pack();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i + 1).append(System.getProperty("line.separator"));
}
area.setText(sb.toString());
try {
Rectangle rect = area.modelToView(10);
area.scrollRectToVisible(rect);
//scroller.getViewport().setViewPosition(new Point(rect.x, rect.y));
} catch (BadLocationException e) {
e.printStackTrace();
}
frame.setVisible(true);
dwga at 2007-7-12 21:35:48 >

# 7
> Anyone else? I find camickr to not be very helpful> in any thread he participates in.... And, I read> alot on these forums...As of today, 'camickr' has been warded 3502 dukes by people who do find his posts helpful. JD has been awarded errrrrrrrrrrr ZERO
# 8
>As of today, 'camickr' has been warded 3502 dukes by people who do find his >posts helpful. JD has been awarded errrrrrrrrrrr ZERO !
Just means camickr spends his time actively posting answers to questions... I, on the other hand, probably don't have the swing experience necessary to answer many of the questions camickr can...
J.D.a at 2007-7-12 21:35:48 >

# 9
> Just means camickr spends his time actively posting answers to questionsSo then maybe I just might know something about what information is necessary in helping to solve the stated problem. Your SSCCE still hasn't been posted, so I guess you really don't want help.
# 10
> the viewport doesn't scroll anywhere, just stays at the bottom.
Confusing. When using the setText(...) method Its my understanding that the viewport should be positioned at the top, not the bottom. This is in fact the way it works in JDK1.4.2 on XP.
The scrollRectToVisible(..) method doesn't do anything and I believe that this is because positioning of the caret to the top is executed after the scrollRectToVisible(). You can try wrapping the scrollRectToVisible() in a SwingUtilities.invokeLater().
However, the setViewPosition(...) does work correctly.
# 11
JFrame frame = new JFrame("Scrolling");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JTextPane txpComment = new JTextPane();
txpComment.setContentType("text/html");
JScrollPane scpComment = new JScrollPane(
txpComment,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
panel.add(scpComment);
frame.add(panel);
frame.pack();
txpComment.setText(bigHTMLDocument);
try {
Rectangle r = txpComment.modelToView(offsetInBackingStringBUffer);
//scpComment.getVerticalScrollBar().setValue(1); -- always scrolls to bottom
//scpComment.getViewport().setViewPosition(new Point(0, 10)); -- always scrolls to bottom
//txpComment.scrollRectToVisible(r); -- always scrolls to bottom
//txpComment.getCaret().setDot(offsetInBackingStringBUffer); -- shows some promise, but jumps way over where it should hit
//scpComment.scrollRectToVisible(r); -- always scrolls to bottom
//txpComment.setCaretPosition(offsetInBackingStringBUffer); -- shows some promise, but jumps way over where it should hit
//txpComment.setCaretPosition(txpComment.modelToView(loc.offset).y); -- shows some promise, but jumps way over where it should hit
//txpComment.moveCaretPosition(loc.offset); -- shows some promise, but jumps way over where it should hit
/*
All the caretPosition function eventually throw exceptions
*/
} catch (BadLocationException e) {
e.printStackTrace();
}
frame.setVisible(true);
J.D.a at 2007-7-12 21:35:48 >

# 12
@dwg
Your example works fine for me, but you need to understand how the scrollRectToVisible() method works. In your example the viewport displays 5 lines of text. Offset 10, is already visible, so there is no need to scroll the viewport. Try changing your code to an offset of say 60 and the viewport should scroll such that the character at offset 60 is displayed on the line at the bottom of the viewport.
@J.D.
You still haven't posted a SSCCE. The code you posted is not compileable. There are no import statements, there is not main method(), You have variable that are not defined. You don't have any data in your text pane.
Your attempt at a SSCCE also does not duplicate the description of your problem. You state the problem is when you search the text pane. This implies to me that the data has been loaded and you have a "search" button which executes some code. This is a different scenario then loading data and scrolling immediately. When you load an HTML document into a JTextPane a lot of parsing needs to be done. Therefore I believe a Thread to started to parse the text and build the Document. So in some cases if you try to invoke the modelToView(...) method you may get exceptions because this code may execute before the model is actually built.
To simulate your test I created a demo with a text pane and a text field. I attached an ActionListener to the text field. So when you enter a number I can then calculate the view based on the offset. This way I am assured that the document has been completely built before the modelToView() method is invoked. Again the code worked fine.
# 13
> @dwg
>
> Your example works fine for me, but you need to
> understand how the scrollRectToVisible() method
> works. In your example the viewport displays 5 lines
> of text. Offset 10, is already visible, so there is
> no need to scroll the viewport. Try changing your
> code to an offset of say 60 and the viewport should
> scroll such that the character at offset 60 is
> displayed on the line at the bottom of the viewport.
Well I'm running this on version 1.5.0_06 and no matter what I set the offset to, the end result is always that the viewport is positioned at the bottom of the text area, i.e. only the bottom 5 lines are visible. This is the case even when I remove all code involved in scrolling, i.e. setText for me scrolls the viewport to the end of the text.
dwga at 2007-7-12 21:35:48 >

# 14
> Well I'm running this on version 1.5.0_06 ...
I'm using 1.4.2 on XP and the behaviour I see makes sense to me. Try rewritting the code using the style of the Swing examples. That is, make sure all the code is executed in the GUI EDT by using the SwingUtilities.invokeLater(...) method.
Another option is to try loading the text into the text pane using the read() method. Using your text String you can create a StringReader and then use the reader as input to the read(..) method.
Thats all I can think of.
# 15
> > Well I'm running this on version 1.5.0_06 ...
>
> I'm using 1.4.2 on XP and the behaviour I see makes
> sense to me. Try rewritting the code using the style
> of the Swing examples. That is, make sure all the
> code is executed in the GUI EDT by using the
> SwingUtilities.invokeLater(...) method.
My example was already doing this.
> Another option is to try loading the text into the
> text pane using the read() method. Using your text
> String you can create a StringReader and then use the
> reader as input to the read(..) method.
Right that worked, thanks.
Strange though that the behavior is so different.
dwga at 2007-7-21 22:54:08 >

# 16
> My example was already doing this.
Well, the code you posted was incomplete, which is why I always ask for a SSCCE so I don't have to guess what you are doing.
> Strange though that the behavior is so different.
Seems, like a bug to me since it work differently in the different versions.
# 17
> Well, the code you posted was incomplete, which is
> why I always ask for a SSCCE so I don't have to guess
> what you are doing.
Right, just realized that I only posted part of it, sorry about that.
> Seems, like a bug to me since it work differently in
> the different versions.
Seems that way to me too.
dwga at 2007-7-21 22:54:08 >
