我想將工具提示添加到(不可編輯)JEditorPane
中的超鏈接。我在網上發現了一些提示,但沒有一個適合我。這是我目前的做法:在鼠標懸停時檢索JEditorPane中超鏈接的標題屬性
jEditorPaneIsFollower.addMouseMotionListener(new java.awt.event.MouseMotionListener() {
@Override
public void mouseMoved(java.awt.event.MouseEvent evt) {
int pos = jEditorPaneIsFollower.viewToModel(evt.getPoint());
if (pos >= 0) {
HTMLDocument hdoc = (HTMLDocument)jEditorPaneIsFollower.getDocument();
javax.swing.text.Element e = hdoc.getCharacterElement(pos);
AttributeSet a = e.getAttributes();
String href = (String) a.getAttribute(javax.swing.text.html.HTML.Attribute.TITLE);
if (href != null) {
jEditorPaneIsFollower.setToolTipText(href);
} else {
jEditorPaneIsFollower.setToolTipText(null);
}
}
else {
jEditorPaneIsFollower.setToolTipText(null);
}
}
@Override
public void mouseDragged(java.awt.event.MouseEvent e) {
//
}
});
我的編輯窗格中的初始化:
jEditorPaneIsFollower.setEditable(false);
jEditorPaneIsFollower.setContentType("text/html");
jEditorPaneIsFollower.setDocument(new HTMLDocument());
jEditorPaneIsFollower.setEditorKit(new HTMLEditorKit());
編輯窗格中的內容如下:
<html>
<head>
</head>
<body>
<table>
<tr>
<td width="1%" valign="top">
Übergeordnet:
</td>
<td valign="top">
<a href="#cr_288" alt="DRGs als Prozesssteuerung" title="DRGs als Prozesssteuerung">288</a>
</td>
</tr>
</table>
</body>
</html>
從調試,我看到pos
當我將鼠標移到編輯器窗格上時總是發生變化,但字符元素e
始終爲null
。
所以我的問題是:
- 我需要編輯器工具包和文件類型設置爲HTML初始化編輯器窗格是什麼時候?
- 將鼠標移到編輯器窗格上時,如何獲取正確的元素並訪問title屬性以將其設置爲工具提示?
非常感謝,作品完美! – Daniel 2014-11-03 09:26:22
很高興幫助... – MadProgrammer 2014-11-03 10:02:06