Search code examples
javaswingjtextpane

Problem with JTextPane - cursor Position at the end


I have a JTextpane that I had populated it from DB.

The problem is when I set text, and when it's long, the JTextPane show the text from the end like in the snapshot.

How can I do ?? I tried setCursor() but I seem it isn't the right method.

enter image description here


Solution

  • You can do something like:

    JTextPane textPane = new JTextPane();
    DefaultCaret caret = (DefaultCaret)textPane.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    textPane.setText(...);
    

    See Text Area Scrolling for more info.