使用 Tab 键从 JTextArea 移动焦点
Posted
技术标签:
【中文标题】使用 Tab 键从 JTextArea 移动焦点【英文标题】:Moving focus from JTextArea using tab key 【发布时间】:2010-10-06 06:24:28 【问题描述】:如前所述,我想更改 JTextArea
中的默认 TAB 行为(使其类似于 JTextField
或类似组件)
这是事件动作
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,但没有任何乐趣。
有什么想法吗?
快速编辑:我也试过用requestFocus()
代替transferFocus()
【问题讨论】:
非常类似于***.com/questions/5042429/… 【参考方案1】:根据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);
注意patch()
可以更短,根据the comments 中的Joshua Goldberg,因为目标是恢复被JTextArea
覆盖的默认行为:
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
这用于问题“How can I modify the behavior of the tab key in a JTextArea
?”
previous implementation 确实涉及Listener
,而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();
可能是您错过了使其适用于您的情况的原因。
【讨论】:
需要更改为 getKeyCode() 和 evt.consume() - evt.consume() 摆脱了制表符并使用 getKeyCode() 允许它使用制表符成功移动焦点。非常感谢:) 不客气。我想知道第一个实现(修改组件的 FocusTraversalKeys 的那个)在某种程度上不是“更干净”... 哇,答案很好,对细节的关注始终如一:)patch
可以更简单,因为您想恢复被 JTextArea 覆盖的默认行为。仅 2 行:component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD,BACKWARD_TRAVERSAL_KEYS, null)
见 ***.com/a/5043957/411282以上是关于使用 Tab 键从 JTextArea 移动焦点的主要内容,如果未能解决你的问题,请参考以下文章
text 使用Tab键移动焦点并计算Web连续表单上的按钮数。 #SenseTalk
Qt Quick Controls 2 TextArea`tabChangesFocus`,如何使用Tab键更改焦点,而不是键入Tab字符