Swing/HyperlinkLabel のバックアップの現在との差分(No.2)
TITLE:Hyperlinkを、JLabel、JButton、JEditorPaneで表示
Hyperlinkを、JLabel、JButton、JEditorPaneで表示
編集者:Terai Atsuhiro~
作成日:2007-11-26
更新日:2024-02-03 (土) 14:04:50
概要
Hyperlink
を、JLabel
、JButton
、JEditorPane
で表示し、それぞれクリックした時のイベントを取得します。
概要
Hyperlinkを、JLabel、JButton、JEditorPaneで表示し、それぞれクリックした時のイベントを取得します。Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class URILabel extends JLabel {
private final String href;
public URILabel(String href) {
super("<html><a href='"+href+"'>"+href+"</a>");
super("<html><a href='" + href + "'>" + href + "</a>");
this.href = href;
setCursor(new Cursor(Cursor.HAND_CURSOR));
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {open(href);}
@Override public void mousePressed(MouseEvent e) {
open(href);
}
});
}
}
#spandel
#spanend
#spanadd
// ...
#spanend
#spanadd
#spanend
JButton button = new JButton(a);
button.setUI(LinkViewButtonUI.createUI(button));
#spanadd
// ...
#spanend
#spanadd
#spanend
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(new Cursor(Cursor.HAND_CURSOR));
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) {
@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);
//...
#spandel
// ... }}
#spanend
#spandel
JEditorPane editor = new JEditorPane("text/html", "<html><a href='"+MYSITE+"'>"+MYSITE+"</a>");
#spanend
#spanadd
JEditorPane editor = new JEditorPane(
#spanend
"text/html", "<html><a href='" + MYSITE + "'>" + MYSITE + "</a>");
editor.setOpaque(false);
#spandel
//editor.setBackground(getBackground());
#spanend
#spandel
editor.setEditable(false); //REQUIRED
#spanend
#spanadd
// editor.setBackground(getBackground());
#spanend
#spanadd
// editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
#spanend
#spanadd
editor.setEditable(false); // REQUIRED
#spanend
editor.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
java.awt.Toolkit.getDefaultToolkit().beep();
@Override public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
Toolkit.getDefaultToolkit().beep();
}
}
});
- &jnlp;
- &jar;
- &zip;
解説
上記のサンプルでは、リンクをブラウザで開く(Desktopでブラウザを起動)代わりに、beep音を鳴らしています。解説
-
JLabel
+MouseListener
-
JLabel
にMouseListener
を設定 - リンクの表示には
<a>
タグを使用
-
-
JButton
+ButtonUI
-
JButton
に文字の描画を変更するButtonUI
を設定-
Rollover
: アンダーライン -
Pressed
: 黒
-
-
-
JEditorPane
+HyperlinkListener
- 編集不可にした
JEditorPane
にHyperlinkListener
を設定 - リンクの表示には
<a>
タグを使用 - 文字列選択してコピーが可能
- 編集不可にした
- JLabel+MouseListener
- JLabelにMouseListenerを設定しています。
- リンクの表示には<a>タグを使っています。
- -
-
このサンプルではクリックされた時に実行するイベントはリンクをブラウザで開く場合の処理はDesktopでブラウザを起動などを参照beep
音を鳴らすだけ
- JButton+ButtonUI
- JButtonに、文字の描画を変更するButtonUIを設定しています。
- Rollover: アンダーライン
- Pressed: 黒
- JButtonに、文字の描画を変更するButtonUIを設定しています。
参考リンク
- JEditorPane (Java Platform SE 8)
- Link Buttons in Swing - Santhosh Kumar's Weblog
-
JButton
+MatteBorder
でリンクを表現するサンプルがある
-
- TransferHandlerでHyperlinkをブラウザにドロップ
- Htmlを使ったJLabelとJEditorPaneの無効化
- JEditorPane+HyperlinkListener
- 編集不可にしたJEditorPaneにHyperlinkListenerを設定しています。
- リンクの表示には<a>タグを使っています。
- 選択してコピーできます。