用户关闭应用程序窗口时,是否有一种优雅的方法可以使JTable停止编辑? [英] Is there an elegant way to make JTable stop editing when user closes the application window?

查看:59
本文介绍了用户关闭应用程序窗口时,是否有一种优雅的方法可以使JTable停止编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户关闭主应用程序窗口时,我试图找到一种优雅的方法来使JTable停止单元格编辑(实际上取消它).我知道可以使用WindowAdapter来完成类似的操作,但是要使其正常工作,我需要对窗口的引用.问题是我有时没有它.

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.

推荐答案

到目前为止,这是我最满意的解决方案:

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

我所有的JTable对象都使用以下方法将自己安装为HierarchyListener,以处理HierarchyEvent:

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

使用层次结构侦听器也至关重要,因为我的JTable对象从可停靠变为可停靠(我们使用了出色的停靠框架此处),并且可以在不同时间位于不同的窗口中.

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.

这篇关于用户关闭应用程序窗口时,是否有一种优雅的方法可以使JTable停止编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆