Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Strange behavior trying to set a scroll bar position

Thread view: 
Matt Brown - 23 Feb 2005 02:21 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.