Take the 2-minute tour ×

I have a JTextArea. I have a function that selects some amount of text when some combination is called. It's done properly. The thing is, I want to move caret to the selection beginning when some text is selected and VK_LEFT is pressed. KeyListener is implemented properly, I tested it in other way. The thing is, that when I write following code:

@Override public void keyPressed( KeyEvent e) {

and add an instance of this listener to mainarea, select some text (using my function) and press left arrow key, the caret position is set to the end of selection... And I wont it to be in the beginning... What's the matter? :S

share|improve this question
don't use KeyListeners, it's a no-no-never ;-) instead, use keybindings .. –  kleopatra May 10 '11 at 14:18
IIRC, you can set the caret to the beginning when you set the selection. For that you have to set selection from the end position to the start position. Thus, when user presses the left key, the caret will be at the start of slection. Isn't it what you want to achieve? –  jfpoilpret May 10 '11 at 14:41
add comment

1 Answer

up vote 5 down vote accepted

Here's a code snippet

    Action moveToSelectionStart = new AbstractAction("moveCaret") {

        public void actionPerformed(ActionEvent e) {
            int selectionStart = textComponent.getSelectionStart();
            int selectionEnd = textComponent.getSelectionEnd();
            if (selectionStart != selectionEnd) {

        public boolean isEnabled() {
            return textComponent.getSelectedText() != null;
    Object actionMapKey = "caret-to-start";
    textComponent.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), actionMapKey);
    textComponent.getActionMap().put(actionMapKey, moveToSelectionStart);

Note: it's not recommended to re-define typically installed keybindings like f.i. any of the arrow keys, users might get really annoyed ;-) Better look for some that's not already bound.

share|improve this answer
+1, for Key Bindings. Hopefully one of these days people will forget about KeyListeners. Although I would extend TextAction to be consistent with the Actions defined in the editor kits. Then you can use getFocusedComponent() to get the text component to act on. –  camickr May 10 '11 at 14:53
@camickr - good point! –  kleopatra May 10 '11 at 15:11
I guess I need to clarify myself again :) In general, I would extend AbstractAction when using Key Bindings on Swing components. However, when adding actions to text components, I would extend TextAction. –  camickr May 10 '11 at 15:18
@camickr - that's how I understood you :-) Here the requirement is a bit pathological, anyway. –  kleopatra May 10 '11 at 15:25
@harmanjd that's the old api (deprecated? don't remember), for textComponents only. The newer style keyBindings are for all JSomething and more flexible. Note that internally, everything registered to a keyMap is wrapped to fit into the input/actionMap –  kleopatra Dec 19 '12 at 15:28
show 2 more comments

Your Answer


