Swing/CaretPosition のバックアップの現在との差分(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CaretPosition へ行く。
- 1 (2005-08-01 (月) 02:22:59)
- 2 (2005-08-01 (月) 02:29:18)
- 3 (2005-10-13 (木) 16:09:46)
- 4 (2006-02-13 (月) 16:42:25)
- 5 (2006-02-27 (月) 15:30:34)
- 6 (2006-02-27 (月) 16:51:05)
- 7 (2006-04-12 (水) 19:35:27)
- 8 (2007-03-02 (金) 16:16:07)
- 9 (2007-08-04 (土) 15:05:48)
- 10 (2009-11-06 (金) 18:29:44)
- 11 (2013-03-27 (水) 16:42:08)
- 12 (2013-07-31 (水) 23:36:19)
- 13 (2013-09-08 (日) 02:32:50)
- 14 (2014-11-25 (火) 03:03:31)
- 15 (2014-12-28 (日) 15:27:22)
- 16 (2015-03-13 (金) 13:08:39)
- 17 (2017-02-09 (木) 15:27:01)
- 18 (2017-04-07 (金) 13:51:51)
- 19 (2017-12-24 (日) 15:14:29)
- 20 (2019-08-30 (金) 18:04:33)
- 21 (2021-04-22 (木) 21:54:03)
- 追加された行はこの色です。
- 削除された行はこの色です。
#navi(../) *JTextPaneで最終行に移動 [#rf478a7f] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2005-08-01~ 更新日:&lastmod; --- category: swing folder: CaretPosition title: JTextPaneで最終行に移動 tags: [JTextPane, JTextComponent, Caret, Document] author: aterai pubdate: 2005-08-01T02:22:59+09:00 description: CaretPositionを指定してJTextPaneの最終行に移動します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTId9wo-yI/AAAAAAAAAS0/GZbZiJfMOwI/s800/CaretPosition.png --- * 概要 [#summary] `CaretPosition`を指定して`JTextPane`の最終行に移動します。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTId9wo-yI/AAAAAAAAAS0/GZbZiJfMOwI/s800/CaretPosition.png) **概要 [#zab9af94] CaretPositionを指定してJTextPaneの最終行に移動します。 * サンプルコード [#sourcecode] #code(link){{ Document doc = jtp.getDocument(); try { doc.insertString(doc.getLength(), text + "\n", null); jtp.setCaretPosition(doc.getLength()); } catch (BadLocationException ex) { ex.printStackTrace(); } }} http://terai.xrea.jp/swing/caretposition/screenshot.png * 解説 [#explanation] 上記のサンプルでは、`Document`に文字列と改行を追加した後、その`Document`の一番最後に`JTextComponent#setCaretPosition(int)`メソッドで`Caret`を移動しています。 **サンプルコード [#i47b5978] Document doc = jtp.getDocument(); try{ doc.insertString(doc.getLength(), str+"\n", null); jtp.setCaretPosition(doc.getLength()); }catch(BadLocationException e) {} - `JTextComponent`内での改行は常に`\n`のため`System.getProperties("line.separator")`で取得したり`\r\n`を考慮する必要はない - 現在の`Caret`位置の行番号を取得する場合のサンプル #code{{ public static int getLineAtCaret(JTextComponent component) { int caretPosition = component.getCaretPosition(); Element root = component.getDocument().getDefaultRootElement(); return root.getElementIndex(caretPosition) + 1; } }} -[[サンプルを起動>http://terai.xrea.jp/swing/caretposition/sample.jnlp]] -[[jarファイル>http://terai.xrea.jp/swing/caretposition/sample.jar]] -[[ソース>http://terai.xrea.jp/swing/caretposition/src.zip]] * 参考リンク [#reference] - [[JScrollPaneのオートスクロール>Swing/AutoScroll]] - [https://community.oracle.com/thread/1393939 Swing - Line Number in JTextPane] - [https://stackoverflow.com/questions/1627028/how-to-set-auto-scrolling-of-jtextarea-in-java-gui How to set AUTO-SCROLLING of JTEXTAREA in Java GUI? - Stack Overflow] **解説 [#mee5ef8f] 上記のサンプルでは、Documentに文字列と改行((JTextPaneでは改行は"\n"であり、System.getProperties("line.separator")としたり、"\r\n"にする必要はない))を追加した後、そのDocumentの一番最後にJTextComponent#setCaretPosition(int)メソッドでキャレットを移動することで最終行への自動スクロールを行っています。 **参考リンク [#tea24622] -[[JScrollPaneのオートスクロール>http://terai.xrea.jp/Swing/AutoScroll.html]] **コメント [#f7f203e1] * コメント [#comment] #comment #comment