0

Scenario is like this: user edits a cell, and presses 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. 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? :)

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...

Edit 2: I would normally do this in my own cell-editor implementation, and prevent commit if validation fails. Unfortunately, in this special case I have to call postUpdateTestFailed() after the change is committed... And then position cursor to the previously edited cell. Weird thing is that I can move to any other cell, but the last edited one!! I consider this a bug.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 21 '12 at 16:26
  • Andrew, if someone needs a SSCCE for the question above, then that person should not bother answering... Just ignore my question, and everything will be fine. But I will make an edit about it now. – DejanLekic Feb 21 '12 at 16:33
  • 1
    *"if someone needs a SSCCE"* Who said I needed it? It is just that an SSCCE *encourages* me to look more deeply into a problem. BTW - do you *need* an answer? – Andrew Thompson Feb 21 '12 at 17:29
  • 1
    sounds weird (+1 for @AndrewThompson suggestion, looks like you don't really want me looking into that ;) – kleopatra Feb 22 '12 at 09:56

2 Answers2

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • trashgod, i am aware of what you said - i do the same myself. Unfortunately, in this special case I have to call `postUpdateTestFailed()` *after* the change is committed... And then position cursor to the previously edited cell. Weird thing is that I can move to *any other cell*, but the last edited one!! I consider this a bug. – DejanLekic Feb 22 '12 at 09:12
2

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

    @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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Thanks Kleopatra - btw, did you notice the fact that you can easily move to ANY other cell, but the last edited one!? :) I still see it as a bug, but you are absolutely right that my listener is notified before JTable, i forgot about that... – DejanLekic Feb 22 '12 at 11:35
  • didn't try, seeing no reason for doing so :-) And no, wouldn't consider it a bug: the exact behaviour is undefined as long as the internal update isn't complete – kleopatra Feb 22 '12 at 11:46
  • Try, say something like `areenTable.changeSelection(currentRow, currentColumn-1, false, false);` - it will work, or, the cell after with `currentColumn+1`. It all works, but the actually edited cell does not. :) – DejanLekic Feb 22 '12 at 11:50
  • _undefined_ means ... well ... there's no way to reliably predict what happens, if it works (as in "as you expect") that's purely accidental :-) – kleopatra Feb 22 '12 at 11:57