• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JTextPaneに修飾した文字列を挿入 [#md42acc5]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-01-13~
更新日:&lastmod;

#contents
**概要 [#p28a05e7]
JTextPaneに、スタイル付けした文字列を挿入して、ログ風に表示します。

http://terai.xrea.jp/swing/styleconstants/screenshot.png

**サンプルコード [#s34357fb]
 private void append(final String str, final boolean flg) {
 private void append(final String str, final boolean error) {
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       SimpleAttributeSet sas = null;
       if(!flg) {
       if(!error) {
         //sas = new SimpleAttributeSet(jtp.getCharacterAttributes());
         sas = new SimpleAttributeSet();
         StyleConstants.setForeground(sas, Color.red);
         //StyleConstants.setBold(sas, true);
         //StyleConstants.setFontFamily(sas, "Monospaced");
         //StyleConstants.setFontSize(sas, 32);
       }
       try{
         Document doc = jtp.getDocument();
         doc.insertString(doc.getLength(), str+"\n", sas);
         jtp.setCaretPosition(doc.getLength());
       }catch(BadLocationException e) {}
     }
   });
 }

-[[サンプルを起動>http://terai.xrea.jp/swing/styleconstants/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/styleconstants/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/styleconstants/src.zip]]
**解説 [#r6829207]
StyleConstantsで文字属性をSimpleAttributeSetに設定し、これを文字列と一緒にDocument#insertStringメソッドを使って挿入しています。

//**参考リンク
**コメント [#zd8e1d8b]
#comment