ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 将JXTable与RXTable结合使用

2019-10-09 02:13:09  阅读:258  来源: 互联网

标签:swingx java swing jxtable


问题

我希望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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有