• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Htmlを使ったJLabelとJEditorPaneの無効化
#navi(../)
*Htmlを使ったJLabelとJEditorPaneの無効化 [#g7a086cf]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2007-12-24~
更新日:&lastmod;
---
category: swing
folder: DisabledHtmlLabel
title: Htmlを使ったJLabelとJEditorPaneの無効化
tags: [Html, JLabel, JEditorPane, UIManager, Fixed]
author: aterai
pubdate: 2007-12-24T23:18:44+09:00
description: Htmlを使ったJLabelと、JEditorPaneをsetEnabled(false)で無効にします。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTK9vV2SGI/AAAAAAAAAW0/PIlAG2B9yZA/s800/DisabledHtmlLabel.png
---
* 概要 [#summary]
`Html`を使った`JLabel`と、`JEditorPane`を`setEnabled(false)`で無効にします。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTK9vV2SGI/AAAAAAAAAW0/PIlAG2B9yZA/s800/DisabledHtmlLabel.png)

**概要 [#i3d606f8]
Htmlを使ったJLabelと、JEditorPaneをsetEnabled(false)で無効にします。

#screenshot

**サンプルコード [#m4ad0b68]
#code{{
final JLabel label2 = new JLabel(HTML_TEXT) {
  public void setEnabled(boolean b) {
* サンプルコード [#sourcecode]
#code(link){{
JLabel label2 = new JLabel(HTML_TEXT) {
  @Override public void setEnabled(boolean b) {
    super.setEnabled(b);
    setForeground(b ? (Color) UIManager.get("Label.foreground")
                    : (Color) UIManager.get("Label.disabledForeground"));
    setForeground(b ? UIManager.getColor("Label.foreground")
                    : UIManager.getColor("Label.disabledForeground"));
  }
};
final JEditorPane editor1 = new JEditorPane("text/html", HTML_TEXT);
JEditorPane editor1 = new JEditorPane("text/html", HTML_TEXT);
editor1.setOpaque(false);
editor1.setEditable(false);
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#r2f0a6d6]
-上段左
--通常のJLabelです。無効化すると
* 解説 [#explanation]
- [https://bugs.openjdk.org/browse/JDK-4783068 JDK-4783068 Components with HTML text should gray out the text when disabled - Java Bug System]
-- `JDK 1.7.0-ea-b55`で以下の描画は修正された
-- このサンプルを実行するとスクリーンショットとは異なり無効化ですべての文字列がグレーになる

-上段中
--Htmlタグを使ったJLabelです。無効化しても文字色は変化しません。

-上段右
--Htmlタグを使ったJLabelです。無効化するとき、setForegroundで文字色を変更していまが、<font color='red'>とした文字の色までは変化しません。

-下段左
--Htmlタグを使ったJLabelです。無効化するとき、setForegroundで文字色を変更し、さらに文字色をグレイスケール化しています。このサンプルでは、無効化している時にラベルのサイズを変更できません。

-下段中
--Htmlタグを使ったJEditorPaneです。無効化すると、すべての文字色が変化します。

-下段中
--Htmlタグを使ったJEditorPaneです。無効化すると、すべての文字色が変化します。以下のようにして、JLabelと同じフォントを使用するように設定しています。
----
- 上段左
-- 通常の`JLabel`
-- 無効化すると文字がへこむ
- 上段中
-- `Html`タグを使った`JLabel`
-- 無効化しても文字色は変化しない
- 上段右
-- `Html`タグを使った`JLabel`
-- 無効化するとき`setForeground`で文字色を変更しているが、`<font color='red'>`とした文字の色までは変化しない
- 下段左
-- `Html`タグを使った`JLabel`
-- 無効化するとき`setForeground`で文字色を変更し、さらに文字色をグレースケール化
-- このサンプルでは無効化している時にラベルのテキストやサイズを変更しても表示は更新されない
- 下段中
-- `Html`タグを使った`JEditorPane`
-- 無効化するとすべての文字色が変化
- 下段右
-- `Html`タグを使った`JEditorPane`
-- 無効化するとすべての文字色が変化
-- 以下のようにして`JLabel`と同じフォントを使用するように設定
#code{{
editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editor2.setFont((Font)UIManager.get("Label.font"));
editor2.setFont(UIManager.getFont("Label.font"));
}}

**参考リンク [#o6dbac3d]
-[[[[Bug ID: 4740519 HTML JLabel not greyed out on setEnabled(false)>http://bugs.sun.com/view_bug.do?bug_id=4740519]]]]
-[[Swing - JLabel with html tag can not be disabled or setForegroud?!>http://forum.java.sun.com/thread.jspa?threadID=735190]]
* 参考リンク [#reference]
- [https://bugs.openjdk.org/browse/JDK-4783068 JDK-4783068 Components with HTML text should gray out the text when disabled - Java Bug System]
- [https://community.oracle.com/thread/1377943 Swing - JLabel with html tag can not be disabled or setForegroud?!]
- [[Hyperlinkを、JLabel、JButton、JEditorPaneで表示>Swing/HyperlinkLabel]]
- [[JLabelなどのHtmlレンダリングを無効化>Swing/HtmlDisable]]
-- `Html`レンダリングを無効化してタグを文字列として表示する場合のサンプル

**コメント [#wa1ddeea]
* コメント [#comment]
#comment
#comment