gpt4 book ai didi

java - 更改关键事件的表条目

转载 作者:行者123 更新时间:2023-12-01 15:59:39 27 4
gpt4 key购买 nike

我有一个 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;

}

最佳答案

也许您应该检查积极的事件,而不是检查错误事件:

if (anEvent instanceof KeyEvent) {
// Get the events key
int keyCode = ((KeyEvent) anEvent).getKeyCode();
char keyChar = ((KeyEvent) anEvent).getKeyChar();
resetText = false;
// F2 allowed
if (keyCode == KeyEvent.VK_F2) {
return true;
}
// letters, digits and space allowed
if (Character.isSpaceChar(keyChar) || Character.isLetterOrDigit(keyChar)) {
resetText = true;
return true;
}
return false;
}

您可能需要使用特殊字符(如 _、*、# 等)扩展最后一个 if 列表。

关于java - 更改关键事件的表条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4283973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com