• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JTextPaneで最終行に移動 [#rf478a7f]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-08-01~
更新日:&lastmod;

#contents

**概要 [#zab9af94]
CaretPositionを指定してJTextPaneの最終行に移動します。

http://terai.xrea.jp/swing/caretposition/screenshot.png

**サンプルコード [#i47b5978]
 Document doc = jtp.getDocument();
 try{
   doc.insertString(doc.getLength(), str+"\n", null);
   jtp.setCaretPosition(doc.getLength());
 }catch(BadLocationException e) {}

-[[サンプルを起動>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]]

**解説 [#mee5ef8f]
上記のサンプルでは、Documentに文字列と改行((JTextPaneでは改行は"\n"であり、System.getProperties("line.separator")としたり、"\r\n"にする必要はない))を追加した後、そのDocumentの一番最後にJTextComponent#setCaretPosition(int)メソッドでキャレットを移動することで最終行への自動スクロールを行っています。

上記のサンプルでは、Documentに文字列と改行((JTextPaneでは改行は"\n"であり、System.getProperties("line.separator")としたり、"\r\n"にする必要はない))を追加した後、そのDocumentの一番最後にJTextComponent#setCaretPosition(int)メソッドでキャレットを移動しています。
**参考リンク [#tea24622]
-[[JScrollPaneのオートスクロール>http://terai.xrea.jp/Swing/AutoScroll.html]]


**コメント [#f7f203e1]
#comment