Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to <a href="http://www.koders.com/java/fidEFE6DD3BBD50DC8AAAE60012E847110CBE3EF7A8.aspx?s=mdef%3Ainsert" rel="nofollow noreferrer">this class</a>:</p> <pre><code>/** * 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. * &lt;br/&gt; * Here s how to use this class to override the "component's default" * behavior: * &lt;pre&gt; * JTextArea area = new JTextArea(..); * &lt;b&gt;TransferFocus.patch( area );&lt;/b&gt; * &lt;/pre&gt; * 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&lt;KeyStroke&gt; strokes = new HashSet&lt;KeyStroke&gt;(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB"))); c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes); strokes = new HashSet&lt;KeyStroke&gt;(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB"))); c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes); } } </code></pre> <p>Note that <code>patch()</code> can be even shorter, according to <a href="https://stackoverflow.com/users/411282/joshua-goldberg">Joshua Goldberg</a> in <a href="https://stackoverflow.com/questions/525855/moving-focus-from-jtextarea-using-tab-key/525867#comment37587468_525867">the comments</a>, since the goal is to get back default behaviors overridden by <code>JTextArea</code>:</p> <pre><code>component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERS‌​AL_KEYS, null); component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERS‌​AL_KEYS, null); </code></pre> <p>This is used in question "<a href="https://stackoverflow.com/a/5043957/6309">How can I modify the behavior of the tab key in a <code>JTextArea</code>?</a>"</p> <hr> <p>The <a href="http://www.koders.com/java/fid0F02F3C453624EDF1564C356CC30EFC1561774E9.aspx?s=mdef%3ainsert" rel="nofollow noreferrer">previous implementation</a> involved indeed a <code>Listener</code>, and the a <code>transferFocus()</code>:</p> <pre><code> /** * 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() &gt; 0) a.transferFocusBackward(); else a.transferFocus(); e.consume(); } } </code></pre> <p><code>e.consume();</code> might have been what you missed to make it work in your case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload