TITLE:JTextPaneに修飾した文字列を挿入
#navi(../)
*JTextPaneに修飾した文字列を挿入 [#md42acc5]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-01-13~
更新日:&lastmod;

#contents

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

#screenshot

**サンプルコード [#s34357fb]
 private void append(final String str, final boolean error) {
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       SimpleAttributeSet sas = null;
       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) {}
     }
   });
 }

-&jnlp;
-&jar;
-&zip;

**解説 [#r6829207]
StyleConstantsで文字属性をSimpleAttributeSetに設定し、これを文字列と一緒にDocument#insertStringメソッドを使って挿入しています。

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