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

#contents

**概要 [#p28a05e7]
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) {}
 }
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) {}
}
}}
-&jnlp;
-&jar;
-&zip;

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

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