【问题标题】:Swing Applet losing dirty dataSwing Applet 丢失脏数据
【发布时间】:2011-09-25 03:35:00
【问题描述】:

在工具栏上按下按钮时,会提示用户是否要放弃他的更改(脏数据)。如果他选择“是”表示他想放弃他的更改,则显示的小程序将停止并销毁。或者,如果用户选择“否”表示他不想放弃他的更改,我会欺骗应用程序保存他的更改(脏数据)。我强制 ToolbarController.SAVE 事件绑定到 SaveAction 线程以强制他的更改。

我想留出足够的时间让 SaveAction 线程完成其工作,因此我将代码包装在 SwingUtilities.invokeAndWait 线程方法调用中。

在运行时,脏数据会丢失。 SwingUtilities.invokeAndWait 是在这种情况下使用的正确方法吗?

这里是sn-p的代码:

public void shutdown() {
    if (configurationManager.isModifedConfigurations()) {
          int selection = 999;

        // Discard changes ?
        selection = JOptionPane.showConfirmDialog(null, messages.getString("ConfigPowerbarChangeConfirmMsg"),
                messages.getString("ConfigPowerbarChangeConfirmMsgTitle"),
                JOptionPane.YES_NO_OPTION);

        if (selection == JOptionPane.NO_OPTION) { //Discard Changes
            configurationManager.setModifedConfigurations(false);

            super.shutdown();
            removeBindings();
            stopCurrentApplet();
        } else if (selection == JOptionPane.YES_OPTION) { //Force Save changes
              System.out.println("Catch ApplicationEvent !!! Force Save of Mutable Table fields.");

            try {
                          SwingUtilities.invokeAndWait(new Runnable() {
                              public void run() {
                                    toolbarController = new ToolbarController(ToolbarModelFactory.getSystemwideToolbarModel());
                                      ApplicationEvent evt = new ApplicationEvent(ToolbarController.SAVE, toolbarController);
                                      toolbarController.handleApplicationEvent(evt);
                                   }
                          });
                    } catch (InterruptedException e) {
                          e.printStackTrace();
                    } catch (InvocationTargetException e) {
                          e.printStackTrace();
                    }
             }
    }
}

建议?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java multithreading swing user-interface applet


【解决方案1】:

问题可能是 SwingWorker 没有阻止关闭过程。想象在这个用例中强制使用 SwingWorker 的 GUI 中没有什么可刷新的,我将只运行 SwingWorker 当前运行的代码,但不使用 SwingWorker。这样,认为在 SwingWorker 中正在做的事情会阻塞关闭进程。

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2012-05-02
    • 2016-07-05
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多