Swing/FIFODocument のバックアップソース(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
TITLE:JTextAreaに表示できる行数を制限 #navi(../) RIGHT:Posted by [[aterai]] at 2006-02-27 *JTextAreaに表示できる行数を制限 [#e59bb955] ドキュメントのサイズを一定にして、JTextAreaなど表示できる行数を制限します。[http://forums.sun.com/thread.jspa?threadID=409418 Swing (Archive) - JTextArea Memory Overflow ??]にあるソースコードを参考にしています。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTMafjL8xI/AAAAAAAAAZI/-KMSGPcn0jM/s800/FIFODocument.png) **サンプルコード [#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; 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); } }); }} **解説 [#v8d4dbca] 一行追加された時に、規定の行数を越えている場合は、先頭から一行を削除しています。 上記のサンプルでは、10行以上になると先頭行から削除されていきます。また、複数行の入力やペーストには対応していません。それらにも対応する場合は、[http://forums.sun.com/thread.jspa?threadID=409418 Swing (Archive) - JTextArea Memory Overflow ??]を参考にしてみてください。 **参考リンク [#z483e8cb] -[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などが発生しているのでしょうか?) -- [[aterai]] &new{2006-11-16 (木) 12:39:54}; #comment