Swing/AnchorTextColor のバックアップ(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/AnchorTextColor へ行く。
- 1 (2017-02-02 (木) 15:15:54)
- 2 (2017-03-28 (火) 15:09:20)
- 3 (2017-04-07 (金) 13:51:51)
- 4 (2018-02-06 (火) 15:29:51)
- 5 (2020-02-05 (水) 16:39:16)
- 6 (2021-07-29 (木) 03:57:48)
- 7 (2025-01-03 (金) 08:57:02)
- 8 (2025-01-03 (金) 09:01:23)
- 9 (2025-01-03 (金) 09:02:38)
- 10 (2025-01-03 (金) 09:03:21)
- 11 (2025-01-03 (金) 09:04:02)
- category: swing
folder: AnchorTextColor
title: JLabelで表示するHtmlアンカータグの文字色を変更する
tags: [JLabel, Html, JEditorPane, HTMLEditorKit, StyleSheet]
author: aterai
pubdate: 2015-03-23T00:19:09+09:00
description: JLabelやJEditorPaneで表示されるHtmlアンカータグのデフォルト文字色を変更するテストを行います。
image:
Summary
JLabel
やJEditorPane
で表示されるHtml
アンカータグのデフォルト文字色を変更するテストを行います。
Screenshot

Advertisement
Source Code Examples
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a{color:#FF0000;}");
JLabel label2 = new JLabel("<html><a href='" + MYSITE + "'>" + MYSITE + "</a>");
JLabel label3 = new JLabel("<html><a style='color:#00FF00' href='" + MYSITE + "'>" + MYSITE + "</a>");
View in GitHub: Java, KotlinExplanation
上記のサンプルでは、Customize detault html link color in java swing - Stack Overflowを参考にして、
Html
のアンカータグ(<a href='...'>...</a>
)のデフォルト文字色を変更しています。
HTMLEditorKit#getStyleSheet()
で取得可能なデフォルトのStyleSheet
はグローバルなAppContext
で管理されているため、これにa{color:#FF0000;}
などのルールを追加するとSwing
アプリ全体でアンカータグの文字色が変更されるJLabel
は作成された時点のデフォルトStyleSheet
を使用(一番上のJLabel
はアンカーの文字色は青のまま)するが、JEditorPane
は現在のStyleSheet
を使用(すべてのJEditorPane
でアンカーの文字色は赤になる)する?- 直接
<a style='color:#00FF00'
のようにスタイルを指定した場合は、こちらが優先される