When I display a HTMLDocument in a JEditorPane, how can I get the selected tag of the HTMLDocument on a mouse press?

Sandip Chitale

Well the problem boils down to getting the attributes of the element at the location where the mouse is pressed. To get that info do the following -


MouseEvent me = // mousePressEvent ;
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
Element elem = doc.getCharacterElement(
    editorPane.viewToModel(me.getPoint())
  );

/* You may have to do -
Element elem = doc.getParagraphElement(
    editorPane.viewToModel(me.getPoint())
  );
*/

AttributeSet attr = elem.getAttributes();

// Alternative approach could be 
/*
AttributeSet attr = doc.getLogicalStyle(
    editorPane.viewToModel(me.getPoint())
  );
*/

// Now scan the attribute set for the tag name at that location.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact