Re: How to write a Caret Listener in Java?
A caret listener must implement one method, caretUpdate. The following code is an example of CaretListenerLabel implementation of caretUpdate :
Code:
public void caretUpdate(CaretEvent e) {
int dot = e.getDot();
int mark = e.getMark();
if (dot == mark) {
try {
Rectangle caretCoords = textPane.modelToView(dot);
setText("caret: text position: " + dot +
", view location = [" +
caretCoords.x + ", " + caretCoords.y + "]" +
newline);
} catch (BadLocationException ble) {
setText("caret: text position: " + dot + newline);
}
} else if (dot < mark) {
setText("selection from: " + dot + " to " + mark + newline);
} else {
setText("selection from: " + mark + " to " + dot + newline);
}
}
"When they give you ruled paper, write the other way..." J.R.J.
Bookmarks