#navi(contents-page-name): No such page: ST

2021-12-24 (金) 12:38:13
  • category: swing folder: StyleConstants title: JTextPaneに修飾した文字列を挿入 tags: [JTextPane, StyledDocument] author: aterai pubdate: 2004-01-12 description: JTextPaneにスタイル付けした文字列を挿入してログ風に表示します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTT31r9lEI/AAAAAAAAAlI/7PqL2Aa3UJU/s800/StyleConstants.png

概要

JTextPaneにスタイル付けした文字列を挿入してログ風に表示します。
http://terai.s55.xrea.com/swing/styledlog/screenshot.png

サンプルコード

View in GitHub: Java, Kotlin

解説

上記のサンプルでは、以下の手順でJTextPaneにスタイルを設定した文字列を追加しています。
  • JTextPaneからStyledDocumentを取得
  • 予め設定しておいたエラー表示用の文字属性スタイルをStyledDocument#getStyle("error")メソッドで取得
  • このスタイルと文字列と合わせてDocument#insertString(...)メソッドで挿入

参考リンク

コメント