问题
我想要 JXTable 的功能使用 RXTable 的“编辑时全选”行为.做一个简单的覆盖会很好,但 RXTable 的双击功能不适用于 JXTable。当使用 Button Action 模式时它很好,但是当使用 F2 或双击 JXTable 中的某些东西时会与 RXTable 发生冲突并删除选择,所以我只剩下默认行为。是因为它在内部使用的 GenericEditor 还是其他原因?
如何让 JXTable 在 F2 上全选或双击编辑?
编辑:看起来这只发生在模型具有为 Integer 类型定义的列时。当为字符串或对象列定义时,它会按预期工作。
解决方案
感谢 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();
}
});
}
被How to select all text in a JFormattedTextField when it gets focus?记住了这个把戏- 不处理通过键入开始编辑时的情况,在这种情况下,内容不会被删除(对于普通文本字段),但会添加新键。
关于java - 结合 JXTable 和 RXTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7365397/