使用Tab键从JTextArea移动焦点 [英] Moving focus from JTextArea using tab key
问题描述
如上所述,我想更改 JTextArea
中的默认TAB行为(这样它就像 JTextField
或类似组件)
As stated, I want to change the default TAB behaviour within a JTextArea
(so that it acts like a JTextField
or similar component)
这是事件动作
private void diagInputKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.KEY_PRESSED == java.awt.event.KeyEvent.VK_TAB) {
actionInput.transferFocus();
}
}
这里是听众
diagInput.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
diagInputKeyPressed(evt);
}
});
我也尝试过evt.KEY_TYPED而且没有任何快乐。
I've tried evt.KEY_TYPED as well with no joy.
任何想法?
快速编辑:我还试过 requestFocus()
in地点 transferFocus()
quick edit: I've also tried requestFocus()
in place of transferFocus()
推荐答案
根据此课程:
/**
* 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);
}
}
注意 patch()根据 Joshua Goldberg 的可以更短//stackoverflow.com/questions/525855/moving-focus-from-jtextarea-using-tab-key/525867#comment37587468_525867\">评论,因为目标是取回由<$ c覆盖的默认行为$ c> JTextArea
:
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_TRAVERSAL_KEYS, null);
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
这用于问题如何在 JTextArea
中修改tab键的行为?
This is used in question "How can I modify the behavior of the tab key in a JTextArea
?"
之前的实施确实涉及监听器
,以及a transferFocus()
:
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() ;
可能是您错过的,以使其适用于您的情况。
e.consume();
might have been what you missed to make it work in your case.
这篇关于使用Tab键从JTextArea移动焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!