简体   繁体   中英

Scrolling to the other part of the webpage

I am rendering a webpage and trying to scroll to a location within it. However, the scrolling doesn't work.

This is my code...

import org.lobobrowser.html.*;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.*;
import org.lobobrowser.html.test.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class finall {

    Node goTo;


    public void show(URL url,Node theFinalNode) throws MalformedURLException, IOException, SAXException {
        goTo = theFinalNode;
        String uri=url.toString(); 

        URLConnection connection = url.openConnection();
        InputStream in = connection.getInputStream();
        Reader reader = new InputStreamReader(in);
        InputSource is = new InputSourceImpl(reader, uri);
        UserAgentContext uAgent=new SimpleUserAgentContext();

        final HtmlPanel htmlPanel = new HtmlPanel();
        HtmlRendererContext rendererContext = (HtmlRendererContext) new LocalHtmlRendererContext(htmlPanel, uAgent);
        DocumentBuilderImpl builder = new DocumentBuilderImpl(uAgent, rendererContext);
        Document document = builder.parse(is);

        JFrame frame = new JFrame();
        frame.getContentPane().add(htmlPanel);
        htmlPanel.setDocument(document, rendererContext);
        frame.setSize(300, 450);
        frame.setVisible(true);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            htmlPanel.scrollTo(goTo);
        }
    });

}

Could someone please help me understand why the scrolling doesn't work.

I think maybe it isn't scrolling because your HtmlPanel isn't added to the GUI within a JScrollPane . Try changing the following code...

JFrame frame = new JFrame();
frame.add(new JScrollPane(htmlPanel)); // CHANGED LINE HERE
htmlPanel.setDocument(document, rendererContext);
// Set the size of the JFrame when the root
// component does not have a preferred size.
frame.setSize(300, 450);
frame.setVisible(true);

Now when your htmlPanel.scrollTo(goTo); is executed later on, it should be able to scroll to this location.

It looks to me as if the Node that you are passing in to the show method is not in the document being viewed by the HtmlPanel. In your code you build the document using:

Document document = builder.parse(is);

This will create a new document and a lot of new Nodes associated with it. The parameter theFinalNode will not be part of this document as it was created before the document was created. You will need to extract the Node you want from your new document by calling methods on the document object, or by using something like XPath:

http://www.roseindia.net/tutorials/xPath/java-xpath.shtml

Once you have a Node that is actually part of the viewed document then the scrollTo method should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM