TITLE:JTextPaneに修飾した文字列を挿入
#navi(../)
RIGHT:Posted by [[terai]] at 2004-01-13
*JTextPaneに修飾した文字列を挿入 [#md42acc5]
JTextPaneに、スタイル付けした文字列を挿入して、ログ風に表示します。

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

#screenshot

**サンプルコード [#s34357fb]
#code{{
private void append(final String str, final boolean error) {
  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) {}
}
}}

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

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