将 JXTable 与 RXTable 组合

发布于 2024-12-04 02:55:08 字数 2571 浏览 5 评论 0原文

问题

我想要 JXTable 的功能 具有 RXTable。进行简单的覆盖就可以了,但是 RXTable 的双击功能不适用于 JXTable。当使用“按钮操作”模式时,这很好,但是当使用 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();
        }
    });
}

Problem

I want the capabilities of JXTable with the "select all on edit" behavior of RXTable. Doing a simple override would be fine but the double click function of the RXTable doesn't work with JXTable. When using the Button Action mode it's fine, but when using F2 or a double click something in JXTable clashes with RXTable and remove the selection so I'm left with default behavior. Is it because of the GenericEditor that it uses internally or is it something else?

How can I get JXTable to select all on F2 or double click edit?

EDIT: It looks like this only happens when the model has the column defined for type Integer. It works as expected when it is defined for a String or Object column.

Solution

Thanks to kleopatra's fix I was able to alter the selectAll method so it handles JFormattedTextFields and all cases of editing. Since the original code worked on type to edit I just used the fix for other cases. Here is what I ended up with.

Replace selectAll in RXTable with the following:

/*
 * 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();
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-12-11 02:55:08

对三种选择类型中的两种进行快速破解

       // 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 中的所有文本? - 不处理通过键入开始编辑时的情况,在这种情况下,内容不会被删除(至于一个正常的textfield),但添加了新键。

a quick hack working for two of the three selection types

       // 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();
                }
            });
        }

was remembered of the trick by How to select all text in a JFormattedTextField when it gets focus? - doesn't handle the case when starting edit by typing, in this case the content is not deleted (as for a normal textfield) but the new key is added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文