Swing/HyperlinkListener のバックアップの現在との差分(No.10)
- category: swing folder: HyperlinkListener title: JEditorPaneにリンクを追加 tags: [JEditorPane, Html, HTMLEditorKit, HyperlinkListener, JButton] tags: [JEditorPane, Html, HTMLEditorKit, HyperlinkListener, JButton, Hyperlink] author: aterai pubdate: 2010-05-10T16:07:06+09:00 description: JEditorPaneにリンクを追加します。 description: JEditorPaneに追加したリンクのクリックイベントなどをHyperlinkListenerで処理します。 image:
概要
JEditorPane
にリンクを追加します。
概要
JEditorPane
に追加したリンクのクリックイベントなどをHyperlinkListener
で処理します。
Screenshot
Advertisement
サンプルコード
サンプルコード
final JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.putClientProperty(
JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setText(htmlText);
editorPane.addHyperlinkListener(new HyperlinkListener() {
private String tooltip;
@Override public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JOptionPane.showMessageDialog(
editorPane, "You click the link with the URL " + e.getURL());
} else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
tooltip = editorPane.getToolTipText();
URL url = e.getURL();
editorPane.setToolTipText((url != null) ? url.toExternalForm() : null);
editorPane.setToolTipText(Objects.nonNull(url) ? url.toExternalForm() : null);
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
editorPane.setToolTipText(tooltip);
}
}
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JEditorPane
(HTMLEditorKit
,編集不可)に挿入したタグ<a href='...'>...</a>
のクリックなどを受信するHyperlinkListener
を追加しています。
解説
上記のサンプルでは、JEditorPane
(HTMLEditorKit
,編集不可)に挿入したタグ<a href='...'>...</a>
のクリックなどを受け取るHyperlinkListener
を追加しています。
- -
以下のように、
JButton
などのコンポーネントを使用する方法もあります。
- 編集可
- 部分選択できない
-
ベースラインがうまく揃わない?JTextPaneに追加するコンポーネントのベースラインを揃える のように、JComponent#setAlignmentY(...)
でテキストベースラインに揃えることが可能 - 以下のように、
JButton
などのコンポーネントをアンカータグの代わりに使用する方法もある-
JEditorPane
にJButton
で作成したリンクを追加 - 編集可の
JEditorPane
で使用可能 - リンク文字列の部分選択が不可
-
ベースラインが揃わないJComponent#setAlignmentY(...)
でテキストベースラインに揃えることが可能- サンプルコードは、JTextPaneに追加するコンポーネントのベースラインを揃えるに移動
-
#spanend
#spandel
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
#spanend
#spandel
Style s = doc.addStyle("button", null);
#spanend
#spandel
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
#spanend
#spandel
HyperlinkButton button = new HyperlinkButton(new AbstractAction(LINK) {
#spanend
@Override public void actionPerformed(ActionEvent e) {
AbstractButton b = (AbstractButton) e.getSource();
editorPane.setBackground(b.isSelected() ? Color.RED : Color.WHITE);
JOptionPane.showMessageDialog(
editorPane, "You click the link with the URL " + LINK);
}
#spandel
});
#spanend
#spandel
button.setToolTipText("button: " + LINK);
#spanend
#spandel
button.setOpaque(false);
#spanend
#spandel
StyleConstants.setComponent(s, button);
#spanend
#spandel
try {
#spanend
doc.insertString(doc.getLength(), "\n----\nJButton:\n", null);
doc.insertString(doc.getLength(), LINK + "\n", doc.getStyle("button"));
//doc.insertString(doc.getLength(), "\n", null);
#spandel
} catch (BadLocationException ble) {
#spanend
ble.printStackTrace();
#spandel
}
#spanend
#spandel