问题
im trying to overwrite mouseClicked function of MouseAdapter then i can use it in my program as follow:
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;
import org.lobobrowser.html.HtmlRendererContext;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.domimpl.HTMLLinkElementImpl;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.w3c.dom.html2.HTMLLinkElement;
import org.w3c.dom.html2.HTMLElement;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class test {
public static void main(String[] args) throws SAXException,
IOException, ParserConfigurationException {
String uri = "http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/MouseAdapter.html";
URL url = new URL(uri);
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();
final HtmlRendererContext rendererContext = (HtmlRendererContext)
new LocalHtmlRendererContext(htmlPanel, uAgent);
DocumentBuilderImpl builder = new DocumentBuilderImpl(uAgent, rendererContext);
Document document = builder.parse(is);
JFrame frame = new JFrame();
frame.setContentPane(htmlPanel);
htmlPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("adr is equal to" + rendererContext.getCurrentURL());
System.out.println("Clicked!" + e);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
htmlPanel.setDocument(document, rendererContext);
frame.setSize(400, 800);
frame.setVisible(true);
}
private static class LocalHtmlRendererContext extends SimpleHtmlRendererContext {
public LocalHtmlRendererContext(HtmlPanel contextComponent, UserAgentContext uAgent) {
super(contextComponent);
}
}
}
but it is not working. if i clik on any part of the form, nothing happens.
i tried htmlPanel.addMouseListener(new MouseAdapter() and frame.addMouseListener(new MouseAdapter() both but no help.
EDIT:
i tried your suggestion, and i choosed BrowsertPanel instead of HtmlPanel, then i tried to get its component as follow:
org.lobobrowser.gui.AddressBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
org.lobobrowser.gui.SharedToolBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
org.lobobrowser.gui.FillerComponent[,0,0,0x0,invalid,layout=org.lobobrowser.util.gui.WrapperLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=java.awt.Dimension[width=32767,height=32767],minimumSize=java.awt.Dimension[width=0,height=0],preferredSize=java.awt.Dimension[width=32767,height=32767]]
org.lobobrowser.gui.StatusBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.BevelBorder@c2a132,flags=9,maximumSize=,minimumSize=,preferredSize=]
also i added a navigationListener and when it navigates, i get the event as follow:
org.lobobrowser.ua.NavigationEvent[source=FramePanel[windowId=BrowserPanel.10481519,hashCode=23491286,parent=org.lobobrowser.gui.FillerComponent[,0,25,584x316,layout=org.lobobrowser.util.gui.WrapperLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=java.awt.Dimension[width=32767,height=32767],minimumSize=java.awt.Dimension[width=0,height=0],preferredSize=java.awt.Dimension[width=32767,height=32767]]]]
but i feel im lost in these components, it seems the FillerComponent or FramePanel are responsible for rendering, even so, i have no idea how to add listener to them.
also found this Link that someone described the steps to solve the problem, but seems so complicated to me? is there any pseudocode to distribute?
回答1:
This code (is an SSCCE &) works. Try changing it one line at a time until you see the error manifest.
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
public class test {
public static void main(String[] args) throws Exception {
String uri = "http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/MouseAdapter.html";
final URL url = new URL(uri);
JEditorPane htmlPanel = new JEditorPane(url);
JFrame frame = new JFrame();
frame.setContentPane(htmlPanel);
htmlPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("adr is equal to" + url);
System.out.println("Clicked!" + e);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 800);
frame.setVisible(true);
}
}
Update
..seems bug is from htmlpanel
I did a bit of digging, based on the comment of StanislavL. It seems a SimpleBrowserFrame extends HtmlPanel & provides the method getComponent() which..
Gets the component that renders the frame. ..
Perhaps if a SimpleBrowser was used, you could add the listener to the child and it would work. Admittedly I am just guessing after a quick trawl though the docs.
回答2:
Just a guess. Check children components of the htmlPanel. May be they cover whole panel. Try to attach the listener to all subcomponents after the HtmlPanel is filled with content.
来源:https://stackoverflow.com/questions/9870527/addmouselistener-to-a-program