编程语言
首页 > 编程语言> > java – 将JXTable与RXTable结合使用

java – 将JXTable与RXTable结合使用

作者:互联网

问题

我希望JXTable的功能具有RXTable的“select all on edit”行为.做一个简单的覆盖就可以了,但是RXTable的双击功能不适用于JXTable.当使用按钮动作模式时它很好,但是当使用F2或双击JXTable中的某些内容与RXTable冲突并删除选择时,我将保留默认行为.是因为它在内部使用的GenericEditor还是其他东西?

如何让JXTable在F2上选择全部或双击编辑?

编辑:看起来这只发生在模型具有为Integer类型定义的列时.当为String或Object列定义时,它按预期工作.

感谢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?的技巧 – 在通过输入开始编辑时没有处理这种情况,在这种情况下,内容不会被删除(正常文本字段),但新密钥被添加.

标签:swingx,java,swing,jxtable
来源: https://codeday.me/bug/20191009/1876030.html