• 追加された行はこの色です。
  • 削除された行はこの色です。
---
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
---
* 概要 [#summary]
`JEditorPane`で選択した文字のレンダリングに使用する色を`null`にして選択文字色の変更を無効化します。

#download(https://drive.google.com/uc?id=1Jv8dY71xTfQSscUwHdO-aekbqgRTFmfzwA)

* サンプルコード [#sourcecode]
#code(link){{
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));
}}

* 解説 [#explanation]
- 上:
-- 選択した文字をレンダリングする色は、`LookAndFeel`でのデフォルト
--- `WindowsLookAndFeel`の場合、白
-- 選択した文字をレンダリングする色は`LookAndFeel`でのデフォルト
--- たとえば`WindowsLookAndFeel`の場合選択文字色は`Color.WHITE`になる
- 下:
-- 選択した文字のレンダリングを`JTextComponent#setSelectedTextColor(null)`で無効化し、`CSS`で設定した文字色のまま表示
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/JTextComponent.html#setSelectedTextColor-java.awt.Color- JTextComponent#setSelectedTextColor(Color) (Java Platform SE 8)]のドキュメントには、「色にnullを設定することは、`Color.black`と同じです。」と記述されているが、`JEditorPane`では文字列選択しても文字色は変更されなくなる
-- 選択した文字のレンダリングを`JTextComponent#setSelectedTextColor(null)`で無効化して`CSS`で設定した文字色を変更しないよう設定
-- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/JTextComponent.html#setSelectedTextColor-java.awt.Color- JTextComponent#setSelectedTextColor(Color) (Java Platform SE 8)]のドキュメントには「色に`null`を設定することは、`Color.black`と同じです。」と記述されているが、実際の`JEditorPane`で`null`を設定すると選択文字色の変更は無効になる

* 参考リンク [#reference]
- [[JEditorPaneで選択色を半透明化>Swing/SelectionColor]]
- [[JEditorPaneにソースコードをシンタックスハイライトして表示する>Swing/SyntaxHighlightingEditorPane]]
- [https://stackoverflow.com/questions/22605666/java-access-files-in-jar-causes-java-nio-file-filesystemnotfoundexception Java access files in jar causes java.nio.file.FileSystemNotFoundException - Stack Overflow]

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