Alert: we were wrong in closing this, please vote to re-open so can get a proper answer!
Every time I select all (Ctrl+A) text on a text area, it scrolls to the bottom.
Is there anyway to maintain the position or the visible rectangle while still selecting all the contents?
UPDATE
This is not a duplicate of Java / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll? since in here, the text in the textarea is not updated but just selected (i.e. select all).
Update Kleopatra
(not exactly up to netiquette, but you need an answer asap, I assume - so I add it here until the question is re-opened, then move :-)
What you basically need is a custom caret, I don't know if that's the whole story, though, as NavigationFilters might (or not) get into the way: in that caret, override adjustVisibilty to conditionally scroll or not to the new dot. A sample:
public static class MyCaret extends DefaultCaret {
@Override
protected void adjustVisibility(Rectangle nloc) {
if (ignoreAdjustVisibility()) {
return;
}
super.adjustVisibility(nloc);
}
/**
* Returns a boolean to indicate whether to scroll the dot to visible.
* This implementation checks for all-selected, that is mark at the start
* and dot at the end.
* @return
*/
private boolean ignoreAdjustVisibility() {
return (getMark() == 0) &&
(getDot() == getComponent().getDocument().getLength());
}
}
// use in application code
JTextArea text = new JTextArea(10, 20);
DefaultCaret caret = (DefaultCaret) text.getCaret();
MyCaret myCaret = new MyCaret();
// for some reason, the blinkrate isn't installed correctly, so do it manually
myCaret.setBlinkRate(caret.getBlinkRate());
text.setCaret(myCaret);
UPDATE
Thanks kleopatra. You're solution works. Hope this question will be reopened.