• title: JLabelなどのHtmlレンダリングを無効化 tags: [JLabel, Html, JToolTip] author: aterai pubdate: 2008-04-21T13:43:08+09:00

概要

JLabelなどのHtmlレンダリングを無効化して、タグ文字列をそのまま表示します。

サンプルコード

label1.putClientProperty("html.disable", Boolean.TRUE);
label1.setText("<html><font color=red>Html Test</font></html>");
label1.setToolTipText("<html>&lt;html&gt;&lt;font color=red&gt;Html Test&lt;/font&gt;&lt;/html&gt;</html>");
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JLabelなどにputClientProperty("html.disable", Boolean.TRUE)を設定することで、タグをレンダリングせずにそのまま文字列として表示しています。


JLabelに、putClientProperty("html.disable", Boolean.TRUE)としても、その JLabelJToolTipには反映されないので、<html>タグの中で文字実体参照を使っています。


Htmlレンダリングされた文字列を、setEnabled(false)で無効化(灰色にする)する場合は、Htmlを使ったJLabelとJEditorPaneの無効化を参考にしてください。

コメント