---
category: swing
folder: HyperlinkLabel
title: Hyperlinkを、JLabel、JButton、JEditorPaneで表示
tags: [Html, JLabel, JButton, JEditorPane, HyperlinkListener, Hyperlink]
author: aterai
pubdate: 2007-11-26T04:49:23+09:00
description: Hyperlinkを、JLabel、JButton、JEditorPaneで表示し、それぞれクリックした時のイベントを取得します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTOIQH0ABI/AAAAAAAAAb4/9QlzkW-7_Es/s800/HyperlinkLabel.png
---
* Summary [#summary]
`Hyperlink`を、`JLabel`、`JButton`、`JEditorPane`で表示し、それぞれクリックした時のイベントを取得します。
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTOIQH0ABI/AAAAAAAAAb4/9QlzkW-7_Es/s800/HyperlinkLabel.png)
* Source Code Examples [#sourcecode]
#code(link){{
class URILabel extends JLabel {
private final String href;
public URILabel(String href) {
super("<html><a href='" + href + "'>" + href + "</a>");
this.href = href;
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
open(href);
}
});
}
}
// ...
JButton button = new JButton(a);
button.setUI(LinkViewButtonUI.createUI(button));
// ...
class LinkViewButtonUI extends BasicButtonUI {
private final static LinkViewButtonUI linkViewButtonUI = new LinkViewButtonUI();
public static ButtonUI createUI(JButton b) {
b.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
b.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return linkViewButtonUI;
}
private LinkViewButtonUI() {
super();
}
private static Dimension size = new Dimension();
private static Rectangle viewRect = new Rectangle();
private static Rectangle iconRect = new Rectangle();
private static Rectangle textRect = new Rectangle();
@Override public synchronized void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
Font f = c.getFont();
g.setFont(f);
FontMetrics fm = c.getFontMetrics(f);
// ...
}
}
JEditorPane editor = new JEditorPane(
"text/html", "<html><a href='" + MYSITE + "'>" + MYSITE + "</a>");
editor.setOpaque(false);
// editor.setBackground(getBackground());
// editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editor.setEditable(false); // REQUIRED
editor.addHyperlinkListener(new HyperlinkListener() {
@Override public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
Toolkit.getDefaultToolkit().beep();
}
}
});
}}
* Description [#explanation]
* Description [#description]
- `JLabel` + `MouseListener`
-- `JLabel`に`MouseListener`を設定
-- リンクの表示には`<a>`タグを使用
- `JButton` + `ButtonUI`
-- `JButton`に文字の描画を変更する`ButtonUI`を設定
--- `Rollover`: アンダーライン
--- `Pressed`: 黒
- `JEditorPane` + `HyperlinkListener`
-- 編集不可にした`JEditorPane`に`HyperlinkListener`を設定
-- リンクの表示には`<a>`タグを使用
-- 文字列選択してコピーが可能
----
- %%このサンプルではクリックされた時に実行するイベントは`beep`音を鳴らすだけ%% リンクをブラウザで開く場合の処理は[[Desktopでブラウザを起動>Swing/Desktop]]などを参照
* Reference [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JEditorPane.html JEditorPane (Java Platform SE 8)]
- [http://www.jroller.com/santhosh/entry/link_buttons_in_swing Link Buttons in Swing - Santhosh Kumar's Weblog]
-- `JButton`+`MatteBorder`でリンクを表現するサンプルがある
- [[TransferHandlerでHyperlinkをブラウザにドロップ>Swing/DraggableLinkButton]]
- [[Htmlを使ったJLabelとJEditorPaneの無効化>Swing/DisabledHtmlLabel]]
* Comment [#comment]
#comment
#comment