1

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.

lonesome
  • 2,503
  • 6
  • 35
  • 61
  • Are you getting any Exceptions? Have you confirmed that your `run()` code is working (such as by debugging it, or writing out messages to a Log or console?) – wattostudios Apr 30 '12 at 03:02
  • @WATTOStudios no exception, the run() method is working cause if i [put other functions there it would work but the one i needed – lonesome Apr 30 '12 at 03:44

2 Answers2

0

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.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • still same, the strange thing is when i use other mjethod tto scroll which uses Coordinate of X,and Y it words but when use the method which uses a Node to scroll it doesnt, what could be wrong? – lonesome May 01 '12 at 04:12
  • Two possibilities jump to mind straight away. 1. maybe the `scrollTo(goTo)` isn't being run before the `JFrame` is displayed, or at least isn't run before the `JFrame` is layed out. Maybe try putting the `scrollTo(goTo)` in a separate `Thread`, or maybe try `Thread.sleep(5000);` just before the `scrollTo(goTo)`. 2. The other possibility is that the `Node goTo` doesn't have a value, or isn't in the document? – wattostudios May 01 '12 at 12:24
  • im sure the node has value i checked it bfore sending it to the function...lets try the thread.sleep – lonesome May 01 '12 at 12:29
  • Yes, put it in the `run()` method so that it will sleep for 5 second, hopefully giving the GUI enough time to format itself before trying to scroll to the Node. – wattostudios May 01 '12 at 12:49
  • so annoying, still the same result – lonesome May 01 '12 at 12:59
  • how can i put the scrollto() into a seperate thread? i have no idea about it – lonesome May 01 '12 at 13:00
  • Change the line `SwingUtilities.invokeLater(new Runnable() {` to `new Thread(new Runnable() {` and change the line `});` to `}).start();` – wattostudios May 01 '12 at 13:04
  • nope, same problem., any other idea? and is there a posibility to get the coordinate of a node of a dom tree that will be refering a part of the page after laying out? – lonesome May 01 '12 at 13:23
  • Not sure how to get the coords of a Node? Have you tried using the `scrollToElement(String id)` method - does this work? You might also need to call `htmlPanel.setPreferredWidth(int width)` after `htmlPanel.setDocument()` so that it can work out where the Node is on the page. – wattostudios May 01 '12 at 14:23
0

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.

Russ Hayward
  • 5,617
  • 2
  • 25
  • 30
  • yea i was suspicious about it too after reading the API i realized it depends on the Document, i may send the Document that i get the finallNode from it by the function then replace it with the line you mentioned to make it something like this **htmlPanel.setDocument(doc.getOwnerDocument(),rendererContext)** which doc is the rootNode of the DOM that the finalNode is resulted from it, but my internet having some problem so i have to check my changes later on and thanks for making me sure about the document thing :) hope this time it works – lonesome May 02 '12 at 10:14