简体   繁体   English

按下TAB后,如何移回到JTable中的已编辑单元格?

[英]How to move back to the edited cell in a JTable, after TAB is pressed?

Scenario is like this: user edits a cell, and presses TAB. 场景是这样的:用户编辑一个单元格,然后按TAB键。 Editing is stopped, and value is in the model. 停止编辑,并且值在模型中。 However, if postUpdateTestFailed() returns true, I want to move selection back to the just edited cell so user can do some modification. 但是,如果postUpdateTestFailed()返回true,则我想将选择移回刚刚编辑的单元格,以便用户可以进行一些修改。 Problem is, it does not work. 问题是,它不起作用。 It works for anything else but the selected cell in my case... :( Because user presses TAB, focused/selected cell is always the next one. Seems like my selection changing is ignored in this particular case. What am I doing wrong? :) 它适用于除我的情况下所选单元格以外的其他任何内容... :(因为用户按TAB键,所以焦点/所选单元格始终是下一个单元格。在这种特定情况下,好像我的选择更改被忽略了。我在做什么错? :)

public void tableChanged(TableModelEvent tme) {
    int row = tme.getFirstRow();
    int col = tme.getColumn();
    int type = tme.getType();
    switch (type) {
        case TableModelEvent.UPDATE:
            currentRow = areenTable.convertRowIndexToView(row);
            currentColumn = areenTable.convertColumnIndexToView(col);
            if (postUpdateTestFailed()) {
                // if the test fails, I want to move back to the edited cell
                areenTable.changeSelection(currentRow, currentColumn, false, false);
            }
            break;

        // the rest of switch

    } // switch
} // tableChanged() method

Edit 1 : I was hoping that someone experienced the same problem before and solved it somehow... 编辑1 :我希望有人以前遇到过相同的问题并以某种方式解决了该问题...

Edit 2 : I would normally do this in my own cell-editor implementation, and prevent commit if validation fails. 编辑2 :我通常会在自己的单元格编辑器实现中执行此操作,并在验证失败时阻止提交。 Unfortunately, in this special case I have to call postUpdateTestFailed() after the change is committed... And then position cursor to the previously edited cell. 不幸的是,在这种特殊情况下,在更改提交 ,我必须调用postUpdateTestFailed() 。然后将光标定位到先前编辑的单元格上。 Weird thing is that I can move to any other cell , but the last edited one!! 奇怪的是,我可以移动到其他任何单元 ,但是最后一个编辑的单元 I consider this a bug. 我认为这是一个错误。

The approach shown checks data after the edit is committed, which requires navigating back to the most recently edited cell. 所显示的方法提交编辑检查数据,这需要导航回到最近编辑的单元格。 Instead, validate the entered value in a custom TableCellEditor before it concludes, as shown in this example . 而是结束之前在自定义TableCellEditor验证输入的值,如本示例所示。

couldn't resist to try it (sounded just soo weird enough - and yeah, even me has to try it out and see what's happening, so next time ... ;-) 忍不住尝试一下(听起来太奇怪了-是的,甚至我也必须尝试一下, 看看发生了什么,所以下一次... ;-)

The problem is that your listener is notified before the table, that is the internal updates are not yet ready. 问题在于表之前会通知您的侦听器,也就是说内部更新尚未准备好。 To work, make sure that it's processed after the table's internals, by wrapping into invokeLater, something like 要工作,请通过将其包装到invokeLater中,确保在表的内部构造之后对其进行处理

    @Override
    public void tableChanged(TableModelEvent tme) {
        int row = tme.getFirstRow();
        int col = tme.getColumn();
        int type = tme.getType();
        switch (type) {
            case TableModelEvent.UPDATE:
                final int currentRow = table.convertRowIndexToView(row);
                final int currentColumn = table.convertColumnIndexToView(col);
                if (currentRow == 0) {
                    // if the test fails, I want to move back to the edited cell
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            table.changeSelection(currentRow, currentColumn, false, false);

                        }
                    });
                }
                break;

            // the rest of switch

        } // switch
    } // tableChanged() method

BTW, I wouldn't listen for changes on the TableModel (they could happen programatically or for other reasons like changes in an underlying data model), instead listen for changes on the table's editor or editingRow/Column property 顺便说一句,我不会监听TableModel上的更改(它们可能会以编程方式发生,也可能会因为其他原因(例如基础数据模型中的更改)而发生,而是会监听表的editor或editRow / Column属性的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM