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 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

As Maurico and codeka first stated, don't use the default InProc sessions if you
This question probably looks a lot like IDE for ironpython on windows question here
look at this jsFIDDLE sample i want to change the cell background color for
I want to get started doing some game development using Microsoft's XNA. Part of
I want to maintain state in my Perl web app. How can I do
I want to get started game programming in C/C++, or C# with DirectX or
I want to record user states and then be able to report historically based
I want to create a keyboard and mouse hook which will be started as
I already started this in javascript so I don't want to use jquery but
I've recently started working with Aptana and Eclipse Monkey. What I want to do

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.