e1001. overriding the default action of a jtextcomponent.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 53 行
TXT
53 行
The default action in a text component keymap receives all typed characters that are not handled in any inputmap or keymap. This example demonstrates how to wrap the default action of a text component with a custom default action.
JTextArea component = new JTextArea();
Action defAction = findDefaultAction(component);
// Install the overriding default action
component.getKeymap().setDefaultAction(new MyDefaultAction(defAction));
public class MyDefaultAction extends AbstractAction {
Action defAction;
public MyDefaultAction(Action a) {
super("My Default Action");
defAction = a;
}
public void actionPerformed(ActionEvent e) {
// Perform customizations here
// This example upper cases all typed characters
if (e.getActionCommand() != null) {
String command = e.getActionCommand();
if (command != null) {
command = command.toUpperCase();
}
e = new ActionEvent(e.getSource(), e.getID(), command, e.getModifiers());
}
// Now call the installed default action
if (defAction != null) {
defAction.actionPerformed(e);
}
}
}
public Action findDefaultAction(JTextComponent c) {
// Look for default action
// Check local keymap
Keymap kmap = c.getKeymap();
if (kmap.getDefaultAction() != null) {
return kmap.getDefaultAction();
}
// Check parent keymaps
kmap = kmap.getResolveParent();
while (kmap != null) {
if (kmap.getDefaultAction() != null) {
return kmap.getDefaultAction();
}
kmap = kmap.getResolveParent();
}
return null;
}
Related Examples
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?