• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaに表示できる行数を制限
#navi(../)
RIGHT:Posted by [[aterai]] at 2006-02-27
*JTextAreaに表示できる行数を制限 [#e59bb955]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-02-27~
更新日:&lastmod;
ドキュメントのサイズを一定にして、JTextAreaなど表示できる行数を制限します。[http://forums.sun.com/thread.jspa?threadID=409418 Swing (Archive) - JTextArea Memory Overflow ??]にあるソースコードを参考にしています。

#contents
-&jnlp;
-&jar;
-&zip;

**概要 [#g22f601f]
ドキュメントのサイズを一定にして、JTextAreaなど表示できる行数を制限します。[[JTextArea Memory Overflow ??>http://forum.java.sun.com/thread.jspa?threadID=409418]]にあるソースコードを参考にしています。
//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTMafjL8xI/AAAAAAAAAZI/-KMSGPcn0jM/s800/FIFODocument.png)

#screenshot

**サンプルコード [#x24ef034]
#code{{
 jta.setEditable(false);
 jta.getDocument().addDocumentListener(new DocumentListener() {
   public void insertUpdate(DocumentEvent e) {
     final Document doc = jta.getDocument();
     final Element root = doc.getDefaultRootElement();
     if(root.getElementCount()<=maxLines) return;
     SwingUtilities.invokeLater(new Runnable() {
       public void run() {
         removeLines(doc, root);
       }
     });
     jta.setCaretPosition(doc.getLength());
   }
   private void removeLines(Document doc, Element root) {
     Element fl = root.getElement(0);
     try{
       doc.remove(0, fl.getEndOffset());
     }catch(BadLocationException ble) {
       System.out.println(ble);
     }
   }
   public void removeUpdate(DocumentEvent e) {}
   public void changedUpdate(DocumentEvent e) {}
 });
 final Timer timer = new Timer(100, new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     String s = new Date().toString();
     jta.append((jta.getDocument().getLength()>0)?"\n"+s:s);
   }
 });
jta.setEditable(false);
jta.getDocument().addDocumentListener(new DocumentListener() {
  public void insertUpdate(DocumentEvent e) {
    final Document doc = jta.getDocument();
    final Element root = doc.getDefaultRootElement();
    if(root.getElementCount()<=maxLines) return;
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        removeLines(doc, root);
      }
    });
    jta.setCaretPosition(doc.getLength());
  }
  private void removeLines(Document doc, Element root) {
    Element fl = root.getElement(0);
    try{
      doc.remove(0, fl.getEndOffset());
    }catch(BadLocationException ble) {
      System.out.println(ble);
    }
  }
  public void removeUpdate(DocumentEvent e) {}
  public void changedUpdate(DocumentEvent e) {}
});
final Timer timer = new Timer(100, new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    String s = new Date().toString();
    jta.append((jta.getDocument().getLength()>0)?"\n"+s:s);
  }
});
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#v8d4dbca]
一行追加された時に、規定の行数を越えている場合は、先頭から一行を削除しています。

上記のサンプルでは、10行以上になると先頭行から削除されていきます。また、複数行の入力やペーストには対応していません。それらにも対応する場合は、[[JTextArea Memory Overflow ??>http://forum.java.sun.com/thread.jspa?threadID=409418]]を参考にしてみてください。
上記のサンプルでは、10行以上になると先頭行から削除されていきます。また、複数行の入力やペーストには対応していません。それらにも対応する場合は、[http://forums.sun.com/thread.jspa?threadID=409418 Swing (Archive) - JTextArea Memory Overflow ??]を参考にしてみてください。

**参考リンク [#z483e8cb]
-[[JTextArea Memory Overflow ??>http://forum.java.sun.com/thread.jspa?threadID=409418]]
-[http://forums.sun.com/thread.jspa?threadID=409418 Swing (Archive) - JTextArea Memory Overflow ??]

**コメント [#ba2d344b]
- sample実行できないよ -- [[cinik]] &new{2006-11-16 (木) 01:09:03};
-- jnlpファイルの名前をsampleからexampleに変更しているので、一旦キャッシュを消してみるとうまくいくかもしれません。(じゃなくてExceptionなどが発生しているのでしょうか?) -- [[terai]] &new{2006-11-16 (木) 12:39:54};
-- jnlpファイルの名前をsampleからexampleに変更しているので、一旦キャッシュを消してみるとうまくいくかもしれません。(じゃなくてExceptionなどが発生しているのでしょうか?) -- [[aterai]] &new{2006-11-16 (木) 12:39:54};

#comment