java结合了JXTable和RXTable
问题
我想要JXTable的功能和RXTable的“编辑时全部选择”行为。做一个简单的覆盖就可以了,但是RXTable的双击功能不适用于JXTable。当使用按钮操作模式时,这是可以的,但是当使用F2或双击JXTable时,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();
}
});
}
共 (0) 个答案