【问题标题】:Combining JXTable with RXTable将 JXTable 与 RXTable 相结合
【发布时间】:2011-11-13 23:45:33
【问题描述】:

问题

我想要JXTable 的功能以及RXTable 的“编辑时全选”行为。做一个简单的覆盖就可以了,但是 RXTable 的双击功能不适用于 JXTable。使用按钮操作模式时很好,但是当使用 F2 或双击 JXTable 中的某些内容时,会与 RXTable 发生冲突并删除选择,所以我留下了默认行为。是因为它在内部使用的 GenericEditor 还是其他原因?

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

编辑:看起来这仅在模型具有为整数类型定义的列时才会发生。为 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();
        }
    });
}

【问题讨论】:

  • worksforme - 继承 JXTable 并覆盖/添加 RXTable 中的方法与核心 JTable 的行为相同
  • @kleopatra 看看我的编辑。看来这只是整数列的问题。
  • 啊...我明白了,感谢您提供的信息:不同行为的根本原因是 xtable 使用 JFormattedTextField 作为编辑组件,它们处理起来比较棘手。没有方便的解决方案...
  • 没有意识到它使用了JFormattedTextField,没有意义。感谢您的帮助。

标签: java swing swingx jxtable


【解决方案1】:

适用于三种选择类型中的两种的快速破解

       // 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? 记住了这个技巧 - 通过键入开始编辑时不处理这种情况,在这种情况下,内容不会被删除(与普通文本字段一样)但会添加新键。

【讨论】:

  • 非常适合 F2 和双击,我用 selectAll 布局的解决方案更新了我的问题,它允许正常代码在点击编辑时工作,并将您的修复用于所有其他情况.谢谢!
  • 在您输入的字母出现在编辑器中之前似乎有大约 1 秒的延迟。有什么办法让它看起来更快?
  • @Burfee 从未见过任何延迟(在 win jdk6/7 中)-您的上下文是什么?无论如何,没有办法精确调试并找到原因;-)
  • 我正在检查它是否是 KeyEvent.VK_DELETE 以便我可以删除单元格的内容并且单元格编辑器是一个 ComboBox。可能在所有的铸造和检查过程中都会出现延迟。如果您说延迟不应该很明显,则需要进行更多调试。谢谢你的回答。
  • @Burfee 您可能会考虑发布一个新问题(与 SSCCE 一起!) - 柏林这里的天气很糟糕 :)
猜你喜欢
  • 2019-12-02
  • 2012-05-19
  • 2020-05-04
  • 1970-01-01
  • 2014-10-23
  • 2011-08-11
  • 2013-04-13
  • 2017-01-03
  • 1970-01-01
相关资源
最近更新 更多