更改关键事件的表条目
我有一个 JTable,按下按键时会自动开始编辑。已输入的文本也将被删除,因此,如果您开始输入,则只有您输入的内容才会获得当前值。这可行,但我的问题是,每个键都会启动编辑,例如大写锁定、数字锁定、ESC 之类的东西。 如何从该编辑中排除所有类型的控制键,而不一一排除?
这是我已有的代码。它位于与表格相关的TableCellEditor 的方法isCellEditable 中。
// It's a keyevent, so check if it's a key that should start editing
if (anEvent instanceof KeyEvent) {
// Get the events key
int keyCode = ((KeyEvent) anEvent).getKeyCode();
char keyChar = ((KeyEvent) anEvent).getKeyChar();
// Convert character to lower case by first creating a string out of it
keyChar = ("" + keyChar).toLowerCase().charAt(0);
// F2 allowed
if (keyCode == KeyEvent.VK_F2) {
return true;
}
// Keycode is unknown so check if we can at least find the keyChar
if (keyCode == 0 && (keyChar == 'ä' || keyChar == 'ö' || keyChar == 'ü')) {
resetText = true;
return true;
}
// Control characters disallowed
if (keyCode < 32 || keyCode == KeyEvent.VK_WINDOWS
|| keyCode == KeyEvent.VK_CONTEXT_MENU || keyCode == KeyEvent.VK_SCROLL_LOCK
|| keyCode == KeyEvent.VK_NUM_LOCK || keyCode == KeyEvent.VK_META
|| (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12))
return false;
// all other keys are allowed
resetText = true;
return true;
}
I have a JTable that starts the editing automatically when pushing a key. The already inputted text will also be deleted, so that if you start typing only your typing gets the current value. That works, but my problem is, that every key starts that editing, like capslock, numlock, esc and stuff like that.
How can I exclude all kind of control keys from that editing, without excluding one by one?
Here's the code I already have. It's in the Method isCellEditable in the TableCellEditor related to the table.
// It's a keyevent, so check if it's a key that should start editing
if (anEvent instanceof KeyEvent) {
// Get the events key
int keyCode = ((KeyEvent) anEvent).getKeyCode();
char keyChar = ((KeyEvent) anEvent).getKeyChar();
// Convert character to lower case by first creating a string out of it
keyChar = ("" + keyChar).toLowerCase().charAt(0);
// F2 allowed
if (keyCode == KeyEvent.VK_F2) {
return true;
}
// Keycode is unknown so check if we can at least find the keyChar
if (keyCode == 0 && (keyChar == 'ä' || keyChar == 'ö' || keyChar == 'ü')) {
resetText = true;
return true;
}
// Control characters disallowed
if (keyCode < 32 || keyCode == KeyEvent.VK_WINDOWS
|| keyCode == KeyEvent.VK_CONTEXT_MENU || keyCode == KeyEvent.VK_SCROLL_LOCK
|| keyCode == KeyEvent.VK_NUM_LOCK || keyCode == KeyEvent.VK_META
|| (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12))
return false;
// all other keys are allowed
resetText = true;
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您不应该检查错误事件,而应该检查积极事件:
您可能需要使用特殊字符(如 _、*、# 等)扩展最后一个 if 列表。
Maybe instead of checking for false events, you shall check for positive ones:
You may need to extend the last if list with special chars like _, *, #, etc.
您可以使用字符包装器轻松确定按下了哪种键。像 isLetterOrDigit(int codepoint) 这样的方法可能会有用。
You can use the Character wrapper to easily determine what kind of key has been pushed. Methods like isLetterOrDigit(int codepoint) would probably be useful.