Problem
我想要的能力JXTable与“编辑时全选”行为RXTable。进行简单的覆盖就可以了,但是 RXTable 的双击功能不适用于 JXTable。当使用“按钮操作”模式时,这很好,但是当使用 F2 或双击 JXTable 中的某些内容时,会与 RXTable 发生冲突并删除选择,因此我保留默认行为。是因为它内部使用的 GenericEditor 还是其他原因?
如何让 JXTable 在 F2 上全选或双击编辑?
编辑:看起来只有当模型具有为 Integer 类型定义的列时才会发生这种情况。当为字符串或对象列定义时,它会按预期工作。
Solution
感谢 kleopatra 的修复,我能够更改 selectAll 方法,以便它处理 JFormattedTextFields 和所有编辑情况。由于原始代码适用于编辑类型,因此我只是将修复程序用于其他情况。这就是我最终得到的结果。
将 RXTable 中的 selectAll 替换为以下内容:
/*
* Select the text when editing on a text related cell is started
*/
private void selectAll(EventObject e)
{
final Component editor = getEditorComponent();
if (editor == null
|| ! (editor instanceof JTextComponent
|| editor instanceof JFormattedTextField))
return;
if (e == null)
{
((JTextComponent)editor).selectAll();
return;
}
// Typing in the cell was used to activate the editor
if (e instanceof KeyEvent && isSelectAllForKeyEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// If the cell we are dealing with is a JFormattedTextField
// force to commit, and invoke selectall
if (editor instanceof JFormattedTextField) {
invokeSelectAll((JFormattedTextField)editor);
return;
}
// F2 was used to activate the editor
if (e instanceof ActionEvent && isSelectAllForActionEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// A mouse click was used to activate the editor.
// Generally this is a double click and the second mouse click is
// passed to the editor which would remove the text selection unless
// we use the invokeLater()
if (e instanceof MouseEvent && isSelectAllForMouseEvent)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((JTextComponent)editor).selectAll();
}
});
}
}
private void invokeSelectAll(final JFormattedTextField editor) {
// old trick: force to commit, and invoke selectall
editor.setText(editor.getText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
editor.selectAll();
}
});
}
适用于三种选择类型中的两种的快速技巧
// in selectAll(EventObject) special case the formatted early
if (editor instanceof JFormattedTextField) {
invokeSelectAll(editor);
return;
}
private void invokeSelectAll(final JFormattedTextField editor) {
// old trick: force to commit, and invoke selectall
editor.setText(editor.getText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
editor.selectAll();
}
});
}
被记住了这个把戏当 JFormattedTextField 获得焦点时,如何选择它中的所有文本?- 不处理通过键入开始编辑时的情况,在这种情况下,内容不会被删除(与普通文本字段一样),但会添加新键。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)