TITLE:JEditorPaneのHTMLEditorKitにCSSを適用

Usage: #tags(tags)
Posted by at 2009-07-20

JEditorPaneのHTMLEditorKitにCSSを適用

JEditorPaneにStyleSheetを追加したHTMLEditorKitを設定します。

  • &jnlp;
  • &jar;
  • &zip;
StyleSheet.png

サンプルコード

StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: 12pt;}");
styleSheet.addRule(".highlight {color: red; background: green}");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
JEditorPane editor = new JEditorPane();
editor.setEditorKit(htmlEditorKit);
editor.setText(makeTestHtml());
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、クラス名が.highlightの要素の文字色と背景色をCSSで変更しています。

参考リンク

コメント

  • HTMLEditorKitのCSSで、色は3桁表記(color: #RGB) には対応していない?(6桁表記 color:#RRGGBB はOK) -- aterai