• category: swing folder: StyleSheet title: JEditorPaneのHTMLEditorKitにCSSを適用 tags: [JEditorPane, StyleSheet, HTMLEditorKit] author: aterai pubdate: 2009-07-20T14:20:10+09:00 description: JEditorPaneにStyleSheetを追加したHTMLEditorKitを設定します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTT6cwbhCI/AAAAAAAAAlM/PsSYnlumJJg/s800/StyleSheet.png

概要

JEditorPaneStyleSheetを追加したHTMLEditorKitを設定します。

サンプルコード

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で変更しています。

参考リンク

コメント