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

  • SEARCH
  • Home
  • 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 127621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:30:21+00:00 2026-05-11T05:30:21+00:00

As stated, I want to change the default TAB behaviour within a JTextArea (so

  • 0

As stated, I want to change the default TAB behaviour within a JTextArea (so that it acts like a JTextField or similar component)

Here’s the event action

private void diagInputKeyPressed(java.awt.event.KeyEvent evt) {     if(evt.KEY_PRESSED == java.awt.event.KeyEvent.VK_TAB) {         actionInput.transferFocus();     } } 

And here’s the listener

diagInput.addKeyListener(new java.awt.event.KeyAdapter() {     public void keyPressed(java.awt.event.KeyEvent evt) {         diagInputKeyPressed(evt);     } }); 

I’ve tried evt.KEY_TYPED as well with no joy.

Any ideas?

quick edit: I’ve also tried requestFocus() in place of transferFocus()

  • 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. 2026-05-11T05:30:21+00:00Added an answer on May 11, 2026 at 5:30 am

    According to this class:

    /**  * Some components treat tabulator (TAB key) in their own way.  * Sometimes the tabulator is supposed to simply transfer the focus  * to the next focusable component.  * <br/>  * Here s how to use this class to override the 'component's default'  * behavior:  * <pre>  * JTextArea  area  = new JTextArea(..);  * <b>TransferFocus.patch( area );</b>  * </pre>  * This should do the trick.   * This time the KeyStrokes are used.  * More elegant solution than TabTransfersFocus().  *   * @author kaimu  * @since 2006-05-14  * @version 1.0  */ public class TransferFocus {      /**      * Patch the behaviour of a component.       * TAB transfers focus to the next focusable component,      * SHIFT+TAB transfers focus to the previous focusable component.      *       * @param c The component to be patched.      */     public static void patch(Component c) {         Set<KeyStroke>          strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke('pressed TAB')));         c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);         strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke('shift pressed TAB')));         c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);     } } 

    Note that patch() can be even shorter, according to Joshua Goldberg in the comments, since the goal is to get back default behaviors overridden by JTextArea:

    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERS‌​AL_KEYS, null); component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERS‌​AL_KEYS, null); 

    This is used in question ‘How can I modify the behavior of the tab key in a JTextArea?‘


    The previous implementation involved indeed a Listener, and the a transferFocus():

       /**      * Override the behaviour so that TAB key transfers the focus      * to the next focusable component.      */     @Override     public void keyPressed(KeyEvent e) {         if(e.getKeyCode() == KeyEvent.VK_TAB) {             System.out.println(e.getModifiers());             if(e.getModifiers() > 0) a.transferFocusBackward();             else a.transferFocus();              e.consume();         }     } 

    e.consume(); might have been what you missed to make it work in your case.

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

Sidebar

Related Questions

so I have this exposed from and I want to change the default value
i want to change the default selection color of my listview items in my
I want to make the color of a tab change on a certain event.
I want to change the state of an ImageView in the items of a
Lets say that I have an executable and when it is started I want
I want to write a ViewModel that always knows the current state of some
As this is my first post here, I would like to thank all of
This question probably looks a lot like IDE for ironpython on windows question here
I want to change ImageView background (in my case gradient color) using selector state
I want to be able to Change the Colors and font size of the

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.