Swing/FIFODocument のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/FIFODocument へ行く。
- 1 (2006-02-27 (月) 11:11:21)
- 2 (2006-02-27 (月) 15:20:32)
- 3 (2006-11-10 (金) 14:35:54)
- 4 (2006-11-16 (木) 01:09:03)
- 5 (2006-11-16 (木) 12:39:54)
- 6 (2006-11-17 (金) 11:11:11)
- 7 (2007-03-07 (水) 02:35:04)
- 8 (2007-05-07 (月) 13:54:42)
- 9 (2011-12-31 (土) 02:17:45)
- 10 (2013-03-14 (木) 20:49:53)
- 11 (2013-09-01 (日) 01:36:50)
- 12 (2014-11-25 (火) 03:03:31)
- 13 (2014-12-30 (火) 15:32:05)
- 14 (2015-02-18 (水) 17:06:02)
- 15 (2015-02-20 (金) 13:24:46)
- 16 (2015-12-21 (月) 18:50:20)
- 17 (2017-01-19 (木) 11:17:21)
- 18 (2017-03-28 (火) 15:08:28)
- 19 (2017-08-02 (水) 15:35:01)
- 20 (2018-08-07 (火) 14:36:18)
- 21 (2020-08-09 (日) 04:31:29)
- 22 (2021-12-31 (金) 01:43:36)
#navi(../) *JTextAreaに表示できる行数を制限 [#e59bb955] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-02-27~ 更新日:&lastmod; #contents **概要 [#g22f601f] ドキュメントのサイズを一定にして、JTextAreaなど表示できる行数を制限します。[[Java Forums - JTextArea Memory Overflow ??>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=409418]]にあるソースコードを参考にしています。 http://terai.xrea.jp/swing/fifodocument/screenshot.png **サンプルコード [#x24ef034] jta.setEditable(false); jta.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { SwingUtilities.invokeLater(new Runnable() { public void run() { removeLines(jta); } }); } public void removeUpdate(DocumentEvent e) {} public void changedUpdate(DocumentEvent e) {} private void removeLines(JTextArea textArea) { Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); while(root.getElementCount()>maxLines) { Element firstLine = root.getElement(0); try{ doc.remove(0, firstLine.getEndOffset()); }catch(BadLocationException ble) { System.out.println(ble); } } textArea.setCaretPosition(doc.getLength()); } }); final Timer timer = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { if(jta.getDocument().getLength()>0) { jta.append("\n"); } jta.append(new Date().toString()); } }); -[[サンプルを起動>http://terai.xrea.jp/swing/fifodocument/sample.jnlp]] -[[jarファイル>http://terai.xrea.jp/swing/fifodocument/sample.jar]] -[[ソース>http://terai.xrea.jp/swing/fifodocument/src.zip]] **解説 [#v8d4dbca] 一行追加された時に、規定の行数を越えている場合は、先頭から一行を削除しています。 **参考リンク [#z483e8cb] -[[Java Forums - JTextArea Memory Overflow ??>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=409418]] **コメント [#ba2d344b] #comment