覆盖VK_Tab焦点操作 [英] Override VK_Tab Focus action
问题描述
美好的一天!
我想将keyevent监听器添加到jTextField,这样如果用户按下tab键,则插入符号位置将结束jtextField中的文本,这是我的代码:
I am tying to add keyevent listener to jTextField so that if the user pressed the tab key, the caret position will go to the end of the text inside the jtextField, here is my code:
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_TAB){
evt.consume();
jTextField1.setCaretPosition(jTextField1.getText().length());
}
}
但它不起作用。
我怎样才能实现这一目标?
How can i make this happen?
推荐答案
一种方法是:
- 首先,不要使用KeyListener,因为这是一个Swing应用程序,如果它可以,你不应该在Swing应用程序中使用KeyListeners
- 接下来通过
jTextField1.setFocusTraversalKeysEnabled(false);
将JTextField的焦点遍历键启用属性设置为false li>
- 然后使用密钥绑定 ,(再次)不是KeyListener,以更改该组件的Tab键的行为。
- First of all, don't use a KeyListener since this is a Swing application, and you shouldn't use KeyListeners in Swing apps if it can be avoided.
- Next set the focus traversal keys enabled property of your JTextField to false via
jTextField1.setFocusTraversalKeysEnabled(false);
- Then use key bindings, (again) not a KeyListener, to change the behavior of the tab key for that component.
例如:
import java.awt.event.*;
import javax.swing.*;
public class OverrideTab {
private static void createAndShowGui() {
JPanel mainPanel = new JPanel();
final JTextField jTextField1 = new JTextField("This is the text", 20);
mainPanel.add(new JButton("Here just to get focus"));
mainPanel.add(jTextField1);
// just to move the caret to position 0 so we can see the key
// bindings code in action
jTextField1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
jTextField1.setCaretPosition(0);
}
});
// turn tab key as focus traversal off for the component
jTextField1.setFocusTraversalKeysEnabled(false);
// set the key bindings
int condition = JComponent.WHEN_FOCUSED;
InputMap inputMap = jTextField1.getInputMap(condition);
ActionMap actionMap = jTextField1.getActionMap();
String tab = "tab";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), tab);
actionMap.put(tab, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
jTextField1.setCaretPosition(jTextField1.getText().length());
System.out.println("here");
}
});
JFrame frame = new JFrame("OverrideTab");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
当然这段代码会阻止你shift-tab离开JTextField,所以如果这种行为是必要且重要的,你可以使用 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,Collections。< KeyStroke> emptySet())
而不是完全禁用焦点遍历。
Of course this code will prevent you from being able to shift-tab out of the JTextField, and so if this behavior is necessary and important, you could use setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<KeyStroke>emptySet())
instead of completely disabling focus traversal.
此外,您所期望的行为违反了大多数窗口操作系统的标准,因此您需要有一个很好的理由来满足需求这是因为您可能会混淆您的用户。
Also, your desired behavior goes against the standards of most windowing operating systems, so you'll want to have a darn good reason for desiring this because you may confuse your users.
这篇关于覆盖VK_Tab焦点操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!