Is there an elegant way to make JTable stop editing when user closes the application window?
Asked Answered
M

2

5

I am trying to find out an elegant way to make JTable stop cell editing (cancel it actually) when user closes the main application window. I know something like this can be done using the WindowAdapter but for this to work properly I need a reference to the window. Problem is I sometimes do not have it.

Marnimarnia answered 10/9, 2014 at 17:17 Comment(1)
What about SwingUtilities.getWindowAncestor?Equitant
M
4

So far, this is the solution I am most satisfied with:

All my JTable objects install themselves as HierarchyListener(s) with the following method to handle the HierarchyEvent:

public void hierarchyChanged(HierarchyEvent e) {
    Window win = SwingUtilities.getWindowAncestor(this);
    if ((win != null) && (!win.equals(topLevelWindow))) {
        topLevelWindow = win;
        topLevelWindow.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                handleWindowClosing();
            }

        });
    }
}

private void handleWindowClosing() {
    if (table.isEditing()) {
        table.getCellEditor().cancelCellEditing();
    }
}

In the case of the project I work on, cancelling the editing when the application window closes is crucial because it sends the notification that the record is no longer in the editing state...

Using hierarchy listener is also crucial because my JTable objects move from dockable to dockable (we use the excellent Docking Frames here) and can be in different windows at different times.

Marnimarnia answered 11/9, 2014 at 14:47 Comment(2)
I don't see a problem, as long as unconditional cancellation is acceptable; I think you'll still need a Quit handler on Mac OS X.Evacuation
Thanks for letting me know, even though we do not care much about Mac OS X and probably never will.Marnimarnia
E
12

An abrupt exit may need to be handled at several levels.

  • Assuming that the user must navigate out of the table to click on an Exit control, you should get the desired result by setting the table's "terminateEditOnFocusLost" property.

    table.putClientProperty("terminateEditOnFocusLost", true);
    
  • If you have to resort to a WindowListener or similar, you can stop editing explicitly as shown here.

    CellEditor cellEditor = table.getCellEditor();
    if (cellEditor != null) {
        if (cellEditor.getCellEditorValue() != null) {
            cellEditor.stopCellEditing();
        } else {
            cellEditor.cancelCellEditing();
        }
    }
    
  • Because the host owns the frame decorations, some platforms require special handling to intercept the close control, as discussed here.

  • Preferences makes a best effort to persist updated values.

Evacuation answered 10/9, 2014 at 20:9 Comment(3)
I should have mentioned that I already have terminateEditOnFocusLost set... The only case that I do not have handled is user clicks on the [x] button, or presses ALT-F4 ...Marnimarnia
You might also look at a shutdown hook, suggested here.Evacuation
That is exactly what I am doing. - Check my answer.Marnimarnia
M
4

So far, this is the solution I am most satisfied with:

All my JTable objects install themselves as HierarchyListener(s) with the following method to handle the HierarchyEvent:

public void hierarchyChanged(HierarchyEvent e) {
    Window win = SwingUtilities.getWindowAncestor(this);
    if ((win != null) && (!win.equals(topLevelWindow))) {
        topLevelWindow = win;
        topLevelWindow.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                handleWindowClosing();
            }

        });
    }
}

private void handleWindowClosing() {
    if (table.isEditing()) {
        table.getCellEditor().cancelCellEditing();
    }
}

In the case of the project I work on, cancelling the editing when the application window closes is crucial because it sends the notification that the record is no longer in the editing state...

Using hierarchy listener is also crucial because my JTable objects move from dockable to dockable (we use the excellent Docking Frames here) and can be in different windows at different times.

Marnimarnia answered 11/9, 2014 at 14:47 Comment(2)
I don't see a problem, as long as unconditional cancellation is acceptable; I think you'll still need a Quit handler on Mac OS X.Evacuation
Thanks for letting me know, even though we do not care much about Mac OS X and probably never will.Marnimarnia

© 2022 - 2024 — McMap. All rights reserved.