Scrolling to the other part of the webpage

懵懂的女人 提交于 2019-12-24 14:54:52

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10377859/scrolling-to-the-other-part-of-the-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!