TITLE:JEditorPaneのHTMLEditorKitにCSSを適用

Posted by at 2009-07-20

JEditorPaneのHTMLEditorKitにCSSを適用

`JEditorPaneStyleSheetを追加した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`で変更しています。

参考リンク

コメント

  • `HTMLEditorKitCSSで、色は3桁表記(color: #RGB) には対応していない?(6桁表記 color:#RRGGBB`は問題なく使用可) -- aterai