更改关键事件的表条目

发布于 2024-10-04 12:09:46 字数 1261 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2024-10-11 12:09:48

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

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 列表。

Maybe instead of checking for false events, you shall check for positive ones:

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;
}

You may need to extend the last if list with special chars like _, *, #, etc.

寄居人 2024-10-11 12:09:48

您可以使用字符包装器轻松确定按下了哪种键。像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文