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.
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:
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.