让我介绍一下我的问题的场景。我希望在 JTable 中呈现数据库表的内容。有了这个 JTable,我应该能够插入新行、删除行以及更新现有行的字段内容。
\n\n第一个期望的行为是,当单元格获得焦点时,如果它是可编辑的,则它会直接进入编辑模式,并且如果它是字母数字内容,则选择所有内容。(文本、数字、日期等)
\n\n下一个期望的行为是Enter按键作为按键工作Tab,即,按下Enter按键焦点必须向前(从左到右)或向后转移到下一个单元格(如果这是可编辑的,则进入编辑模式)。
为了满足第一个要求,我使用以下方法覆盖 JTable 类的changeSelection 方法。
\n\n@Override\npublic void changeSelection(int row, int column, boolean toggle, boolean extend) {\n super.changeSelection(row, column, toggle, extend);\n if (editCellAt(row, column)) {\n Component editor = getEditorComponent();\n editor.requestFocusInWindow();\n if (editor instanceof JFormattedTextField) {\n ((JFormattedTextField) editor).select(0,\n ((JFormattedTextField) editor).getText().length());\n } else if (editor instanceof JTextField) {\n ((JTextField) editor).selectAll();\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在阅读了大量文档和帖子后,很明显解决问题的最合适方法是通过使用键绑定,基本上,读完之后,解决方案是将键的行为分配给Tab键Enter,我也这么做了。
private void tableConfiguration() {\n //Configuramos la tabla para que …Run Code Online (Sandbox Code Playgroud)