• category: swing folder: SelectedTextColor title: JEditorPaneで選択した文字列の色反転を無効化 tags: [JEditorPane, HTMLEditorKit, StyleSheet] author: aterai pubdate: 2018-12-17T15:32:38+09:00 description: JEditorPaneで選択した文字のレンダリングに使用する色をnullにして選択文字色の変更を無効化します。 image: https://drive.google.com/uc?id=1Jv8dY71xTfQSscUwHdO-aekbqgRTFmfzwA

概要

JEditorPaneで選択した文字のレンダリングに使用する色をnullにして選択文字色の変更を無効化します。

サンプルコード

HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
editor.setEditorKit(htmlEditorKit);
editor.setEditable(false);
editor.setSelectedTextColor(null);
editor.setSelectionColor(new Color(0x64_88_AA_AA, true));
editor.setBackground(new Color(0xEE_EE_EE));
View in GitHub: Java, Kotlin

解説

  • 上:
    • 選択した文字をレンダリングする色は、LookAndFeelでのデフォルト
      • WindowsLookAndFeelの場合、白
  • 下:
    • 選択した文字のレンダリングをJTextComponent#setSelectedTextColor(null)で無効化し、CSSで設定した文字色のまま表示
    • JTextComponent#setSelectedTextColor(Color) (Java Platform SE 8)のドキュメントには、「色にnullを設定することは、Color.blackと同じです。」と記述されているが、JEditorPaneでは文字列選択しても文字色は変更されなくなる

参考リンク

コメント