JAVA : JEditorPane - Possible to intercept mouseclicks on HTML tags?

Discussion in 'Programming' started by kb4, Dec 6, 2009.

  1. #1
    Hi,

    I'm using a JEditorPane to display HTML pages. I'm familiar with the HyperLinkUpdate listener to intercept mouse clicks on HTML links within a page, but I would like to intercept mouse clicks that land on HTML tags such as a table cell or an item in an ordered list.

    1. Can I do this with a JEditorPane ?
    2. If so, is it possible to determine which tag within the page has been clicked ?
    3. Can I then modify the HTML tag e.g. it's border colour ?

    Many thanks
     
    kb4, Dec 6, 2009 IP
  2. gacba

    gacba Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The documentation for JEditorPane doesn't say much. Try installing a MouseListener (at the java.awt.Component level) and see if you get ANY events for clicks with System.out's. My guess is that they're intercepted by the JEditorPane and processed in a way that prevents you from dealing with them.
     
    gacba, Dec 7, 2009 IP
  3. devunion

    devunion Greenhorn

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    Take a look at HTMLEditorKit sources. In public void actionPerformed(ActionEvent e) method you can find the next code:

    		Object href = getAttrValue(attr, HTML.Attribute.HREF);
    		if (href != null) {
    		    if (currentOffset >= currentElement.getStartOffset() &&
    			currentOffset <= currentElement.getEndOffset()) {
    			
    			activateLink((String)href, doc, editor, currentOffset);
    			return;
    		    }
    Code (markup):
    activateLink((String)href, doc, editor, currentOffset) fires HyperlinkEvent for all HyperlinkListener objects.

    I think that you can implement custom HTMLEditorKit and add support of necessary events for your project. Also it has sense to create custom class for JEditorPane.
     
    Last edited: Dec 7, 2009
    devunion, Dec 7, 2009 IP