Hi,
I am writing a swing application that reads xml files and displays xml
source and transformed html in two JEditorPanes, both inside JScrollPanes.
When the file loads, by default the view of the source gets scrolled down
to the bottom. I'm trying to set the scrollbar position at the top, by
calling:
editorScrollPane.getViewport().setViewPosition(new Point(0,0));
However this only works in certain places in my code, and not elsewhere.
For example, in my init() I can do:
...
editorScrollPane.getViewport().add(editorPane, null);
editorPane.setText("some long string \n\n\n\n\n\n\n\n\n\n");
editorScrollPane.getViewport().setViewPosition(new Point(0,0));
...
which works fine. I can also do:
public void resetScrollBarButtonPressed() {
editorScrollPane.getViewport().setViewPosition(new Point(0,0));
}
and when a user presses the button this works too. However, what I want to
do is load a file via menu, and have and then immediately set the scrollbar
to the top of the display in the same method, like this:
//File | Load action performed
public void jMenuFileLoad_actionPerformed(ActionEvent e) {
File file = jFileChooser1.getSelectedFile();
String filename = file.getAbsolutePath();
try {
editorPane.setText("");
// Parse the xml file
document=documentBuilder.parse(file);
// Convert the xml document to string for display
editorPane.setText(writeNode(document, ""));
} catch (IOException ioe) {
log("IOException: " + ioe.getMessage());
} catch (org.xml.sax.SAXException se) {
log("SAXException: " +se.getMessage());
}
editorScrollPane.getViewport().setViewPosition(new Point(0,0));
}
this doesn't work and I have no idea why. I've wasted a couple hours
looking for a solution online and found nothing useful. Can anybody tell me?
I must be missing something basic.
thanks,
matt
John McGrath - 23 Feb 2005 10:52 GMT
> I'm trying to set the scrollbar position at the top, by calling:
>
> editorScrollPane.getViewport().setViewPosition(new Point(0,0));
Try setting the caret position for the JEditorPane to 0.

Signature
Regards,
John McGrath
Matt Brown - 23 Feb 2005 17:56 GMT
That works! great. Although I'm still confused as to why
getViewport().setViewPosition(new Point(0,0)) doesn't work...
Thanks,
matt
John McGrath - 23 Feb 2005 18:44 GMT
> That works! great. Although I'm still confused as to why
> getViewport().setViewPosition(new Point(0,0)) doesn't work...
It does, but it does not last. The JEditorPane asks the JScrollPane
to scroll itself so that the caret position is visible.

Signature
Regards,
John McGrath
Matt Brown - 23 Feb 2005 20:10 GMT