• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display top of JEditorPane/JScrollPane  RSS feed

 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing an application with a gui that displays questions. It shows one question at a time--the user clicks through the test question-by-question. Each question is generated from XML into an html string (using my own code)... and then I put it all inside a JEditorPane which is inside a JScrollPane. Unfortunately, whenever I go to a question, the question window is scrolled all the way down to begin with. So instead of seeing the first part of the question, the user starts out seeing the end of the question and has to scroll up each time.
I am pretty clueless why this happens, although one guess is that it's due to the way I'm filling the content of the JEditorPane.
In any case, is there a simple method that can be called on the JEditorPane or the JScrollPane that will immediately put the user at the top of the question each time?
Thanks,
Andy
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call -


after you set the new text.

There is also a

method that works with references in HTML documents.

-Nate
 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
Thanks for the tips. They make sense and look like they should work, but they're not working for my application.
My JEditorPane is inside a JScrollPane. When I don't use the JScrollPane, the initial layout works even without the tips--except I can't scroll down.
Any ideas for how to get the JScrollPane to display from the top? (Currently it's still displaying the bottom of the html initially.)
Thanks again,
Andy
My code looks like this:
jep = new JEditorPane();
jep.setContentType(HTML_CONTENT_TYPE);
jsp = new JScrollPane(jep);
this.add(jsp, BorderLayout.CENTER);
...
jep.setText(sb.toString());
 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also --
Perhaps I was using the scrollToReference(String) method incorrectly. It was protected, so I couldn't access it. So first I subclassed JEditorPane and overrode it with a *public* version that just calls super.scrollToReference(String).
So I'm actually using this subclassed version of JEditorPane. For the string argument, it's unclear to me what it should be. I'm currently using this:
jep.scrollToReference( "<a href=\"#top_marker\" target=\"_self\">" );
... and in the html I have an anchor right inside the <body> tag that is as follows:
<a name="top_marker">
So, is that right? I bet it isn't, but couldn't find the right way to do it.
- Andy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!