Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4602304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:57:49+00:00 2026-05-21T23:57:49+00:00

I have a JTextArea. I have a function that selects some amount of text

  • 0

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) {
        if(e.getKeyChar()==KeyEvent.VK_LEFT)
            if(mainarea.getSelectedText()!=null)
                mainarea.setCaretPosition(mainarea.getSelectionStart());
    }

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:57:50+00:00Added an answer on May 21, 2026 at 11:57 pm

    Here’s a code snippet

        Action moveToSelectionStart = new AbstractAction("moveCaret") {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectionStart = textComponent.getSelectionStart();
                int selectionEnd = textComponent.getSelectionEnd();
                if (selectionStart != selectionEnd) {
                    textComponent.setCaretPosition(selectionEnd);
                    textComponent.moveCaretPosition(selectionStart);
                }
            }
    
            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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textarea and among free text input I insert some tags that
Is it possible to have something like a JTextArea which will color some keywords
I want to select every textarea that does not have a DIV with class
I have some code that I am using to count characters in an input/textarea
I'll try to ask this in a way that makes some sense. I have
I have a script that appends some rows to a table. One of the
I have a HTML document which llokes like this: <div class=this_can_be_selected>Some text here</div> <div>No
I have saved input from a textarea element to a TEXT column in MySQL.
I have a form with a textarea. Users enter a block of text which
I have a form with a <textarea> and I want to capture any line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.