Swing/FIFODocument のバックアップの現在との差分(No.18)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: FIFODocument title: JTextAreaに表示できる行数を制限 tags: [JTextArea, DocumentListener] author: aterai pubdate: 2006-02-27T11:11:21+09:00 description: ドキュメントのサイズを一定にして、JTextAreaなど表示できる行数を制限します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMafjL8xI/AAAAAAAAAZI/-KMSGPcn0jM/s800/FIFODocument.png --- * 概要 [#summary] ドキュメントのサイズを一定にして、`JTextArea`など表示できる行数を制限します。[https://community.oracle.com/thread/1479784 Swing (Archive) - JTextArea Memory Overflow ??]にあるソースコードを参考にしています。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMafjL8xI/AAAAAAAAAZI/-KMSGPcn0jM/s800/FIFODocument.png) * サンプルコード [#sourcecode] #code(link){{ jta.setEditable(false); jta.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { final Document doc = jta.getDocument(); final Element root = doc.getDefaultRootElement(); if (root.getElementCount() <= maxLines) return; EventQueue.invokeLater(new Runnable() { @Override 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); } } @Override public void removeUpdate(DocumentEvent e) {} @Override public void changedUpdate(DocumentEvent e) {} }); final Timer timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String s = new Date().toString(); jta.append((jta.getDocument().getLength() > 0) ? "\n" + s : s); } Timer timer = new Timer(100, () -> { String s = new Date().toString(); jta.append((jta.getDocument().getLength() > 0) ? "\n" + s : s); }); }} * 解説 [#explanation] 上記のサンプルでは、一行追加した時に規定の行数を越えている場合、先頭の一行を削除する`DocumentListener`を作成し、これを`JTextArea`に設定しています。 上記のサンプルでは、`1`行追加した時に規定の行数を越えている場合、先頭の`1`行を削除する`DocumentListener`を作成し、これを`JTextArea`に設定しています。 - メモ -- `10`行以上になると先頭行から削除 -- 複数行の貼り付けなどには未対応 --- 参考: [https://community.oracle.com/thread/1479784 Swing (Archive) - JTextArea Memory Overflow ??]は、複数行貼り込みに対応している -- `DocumentListener`ではなく、以下のような`DocumentFilter`を設定する方法もある - `10`行以上になると先頭行から削除 - 複数行テキストのペーストには未対応 -- 参考: [https://community.oracle.com/thread/1479784 Swing (Archive) - JTextArea Memory Overflow ??]は複数行貼り込みに対応している - `DocumentListener`ではなく以下のような`DocumentFilter`を設定する方法もある #code{{ //((AbstractDocument) jta.getDocument()).setDocumentFilter(new FIFODocumentFilter()); ((AbstractDocument) ta.getDocument()).setDocumentFilter(new FIFODocumentFilter()); // ... class FIFODocumentFilter extends DocumentFilter { private static final int MAX_LINES = 10; @Override public void insertString( DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { fb.insertString(offset, string, attr); Element root = fb.getDocument().getDefaultRootElement(); if (root.getElementCount() > MAX_LINES) { fb.remove(0, root.getElement(0).getEndOffset()); } } } }} * 参考リンク [#reference] - [https://community.oracle.com/thread/1479784 Swing (Archive) - JTextArea Memory Overflow ??] * コメント [#comment] #comment - `sample`実行できないよ -- &user(cinik); &new{2006-11-16 (木) 01:09:03}; -- `jnlp`ファイルの名前を`sample`から`example`に変更しているので、一旦キャッシュを消してみるとうまくいくかもしれません。(じゃなくて`Exception`などが発生しているのでしょうか?) -- &user(aterai); &new{2006-11-16 (木) 12:39:54}; -- `jnlp`ファイルの名前を`sample`から`example`に変更しているので、一旦キャッシュを消してみるとうまくいくかもしれません。(そうではなく`Exception`などが発生しているのでしょうか?) -- &user(aterai); &new{2006-11-16 (木) 12:39:54}; #comment