How can i find the tag at which my cursor points in JEditorPane
I want to know whether we can find a tag in JEditorPane in java swing where my cursor is pointing.. For ex. following is the text content in my editor pane..
<html>
<head>
<body>
<div><!--Cursor inside the div tag--></div>
</body>
</html>
for instance suppose this is the text inside my editor pane. and my cursor is inside the tag. now what i want is a function that returns me the tag in which the cursor is. Is it possible?
Here cursor is 开发者_如何学Goin <div>
tag so function should return "div"
or "<div>"
.
Yes. DIV
is defined in HTML.Tag
, so you can iterate though the elements of your HTMLDocument
in a CaretListener
. You can get the offsets from the attribute's child element.
HTMLDocument doc=(HTMLDocument)pane.getDocument(); Element elem=doc.getCharacterElement(pane.getCaretPositon()); The take a look at the elem's attributes or elem.getParent() attributes because the char elem is text but the div is parent element's attribute.
精彩评论