如何在JTextField中禁用默认文本字段快捷方式 [英] How to disable default textfield shortcuts in JTextField
问题描述
我有一个自定义的textfield类,它在Swing中扩展了JTextField类。
I have a custom textfield class that extends the JTextField class in Swing.
我需要找到一种方法来禁用Ctrl-A的默认操作(全选) ),Ctrl-H(退格键等),以便包含文本字段的窗口可以将这些快捷方式映射到它想要的任何内容。
I need to find a way to disable the default actions for Ctrl-A (select all), Ctrl-H (backspace) etc, so that the window containing the textfield can map these shortcuts to whatever it wants.
任何帮助将不胜感激。
推荐答案
好的,我自己找到答案:
Okay, found the answer myself:
将以下内容添加到textfield类的初始化方法:
Added the following to an initilization method of the textfield class:
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), "none");
效果是文本字段忽略了快捷方式,并将按键传递给快捷方式处理程序窗口。
The effect is that the textfield ignores the shortcut and lets the keystroke be passed along to the shortcut handler in the window.
这篇关于如何在JTextField中禁用默认文本字段快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!