JTextPaneで最終行に移動

編集者:Terai Atsuhiro
作成日:2005-08-01
更新日:2021-04-22 (木) 21:54:03

概要

CaretPositionを指定してJTextPaneの最終行に移動します。

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

サンプルコード

Document doc = jtp.getDocument();
try{
  doc.insertString(doc.getLength(), str+"\n", null);
  jtp.setCaretPosition(doc.getLength());
}catch(BadLocationException e) {}

解説

上記のサンプルでは、Documentに文字列と改行*1を追加した後、そのDocumentの一番最後にJTextComponent#setCaretPosition(int)メソッドでキャレットを移動しています。

参考リンク

コメント