Swing/HyperlinkLabel のバックアップソース(No.19)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/HyperlinkLabel へ行く。
- 1 (2007-11-26 (月) 04:49:23)
- 2 (2007-11-26 (月) 19:14:45)
- 3 (2007-11-27 (火) 11:11:34)
- 4 (2007-12-04 (火) 15:37:59)
- 5 (2008-01-25 (金) 16:19:16)
- 6 (2008-02-07 (木) 19:04:35)
- 7 (2008-05-19 (月) 14:06:20)
- 8 (2010-03-08 (月) 12:43:52)
- 9 (2010-03-08 (月) 13:45:50)
- 10 (2011-05-20 (金) 15:52:14)
- 11 (2013-01-29 (火) 14:40:32)
- 12 (2014-12-06 (土) 20:44:33)
- 13 (2014-12-14 (日) 14:44:21)
- 14 (2016-02-25 (木) 21:29:48)
- 15 (2016-05-27 (金) 13:23:14)
- 16 (2017-09-01 (金) 17:49:26)
- 17 (2018-09-21 (金) 15:51:21)
- 18 (2019-05-22 (水) 19:35:38)
- 19 (2020-09-23 (水) 00:44:24)
- 20 (2022-05-26 (木) 16:21:30)
- 21 (2024-02-03 (土) 14:04:50)
--- 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] `Hyperlink`を、`JLabel`、`JButton`、`JEditorPane`で表示し、それぞれクリックした時のイベントを取得します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTOIQH0ABI/AAAAAAAAAb4/9QlzkW-7_Es/s800/HyperlinkLabel.png) * サンプルコード [#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); } }); } } }} #code{{ 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); // ... }} #code{{ 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(); } } }); }} * 解説 [#explanation] 上記のサンプルでは、リンクを`JLabel`、`JButton`、`JEditorPane`で表現し、それぞれクリックした時のイベントを取得するテストを行っています。 - `JLabel` + `MouseListener` -- `JLabel`に`MouseListener`を設定 -- リンクの表示には`<a>`タグを使用 - `JButton` + `ButtonUI` -- `JButton`に、文字の描画を変更する`ButtonUI`を設定 --- `Rollover`: アンダーライン --- `Pressed`: 黒 - `JEditorPane` + `HyperlinkListener` -- 編集不可にした`JEditorPane`に`HyperlinkListener`を設定 -- リンクの表示には`<a>`タグを使用 -- 文字列選択してコピーが可能 ---- - 注: -- このサンプルでは、クリックされた時に実行するイベントは`beep`音を鳴らすだけのダミー -- リンクをブラウザで開く場合は、[[Desktopでブラウザを起動>Swing/Desktop]]などを参照 * 参考リンク [#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