安全地打开和关闭模态JDialog(使用SwingWorker) [英] Safely open and close modal JDialog (using SwingWorker)

查看:107
本文介绍了安全地打开和关闭模态JDialog(使用SwingWorker)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法从数据库中获取一些数据并阻止用户在那一刻修改现有数据。

I needed a way to get some data from a database and prevent the user from modifying existing data for that moment.

我创建了一个SwingWorker来进行数据库更新和一个模态JDialog向用户显示正在发生的事情(使用JProgressBar)。模态对话框的defaultCloseOperation设置为DO_NOTHING,因此只能通过正确的调用关闭它 - 我使用 setVisible(false)

I created a SwingWorker to make the db update and a modal JDialog to show user what is going on (with a JProgressBar). The modal dialog has defaultCloseOperation set to DO_NOTHING, so it can only be closed with a proper call - I use setVisible(false).

MySwingWorkerTask myTask = new MySwingWorkerTask();
myTask.execute();
myModalDialog.setVisible(true);

SwingWorker在doInBackground()中执行一些操作,最后调用:

The SwingWorker does some stuff within doInBackground() and lastly it calls:

myModalDialog.setVisible(false);

我唯一担心的问题是:
SwingWorker可能会执行 setVisible(false)在工人生成后的行中 setVisible(true)之前?

My only concern and my question: Is is possible that the SwingWorker executes the setVisible(false) before it is setVisible(true) in the line after worker spawn?

如果是这样, setVisible(true)可以永久阻止(用户无法关闭模态窗口)。

If so the setVisible(true) could block forever (the user can't close the modal window).

我是否必须实现以下内容:

Do I have to implement something as:

while (!myModalDialog.isVisible()) {
    Thread.sleep(150);
}
myModalDialog.setVisible(false);

以确保它实际上会被关闭?

to make sure it will actually get closed?

推荐答案

一般来说,是的。

我要做的是你的 doInBackground 方法是使用 SwingUtilities.invokeLater 来显示对话框并在 done 方法中隐藏对话框。

What I would do is in your doInBackground method is use SwingUtilities.invokeLater to show the dialog and in your done method hide the dialog.

这应该意味着即使对话框没有进入屏幕,你也可以获得对流量的更多控制......

This should mean that even if the dialog doesn't make it to the screen you gain a little more control over the flow...

小问题是你现在必须将对话框传递给工人,以便它可以控制它......

The minor issue is you're now going to have to pass the dialog to the worker so it can gain control over it...

public class TestSwingWorkerDialog {

    public static void main(String[] args) {
        new TestSwingWorkerDialog();
    }
    private JDialog dialog;

    public TestSwingWorkerDialog() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                MyWorker worker = new MyWorker();
                worker.execute();

            }
        });
    }

    public class MyWorker extends SwingWorker<Object, Object> {

        @Override
        protected Object doInBackground() throws Exception {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    getDialog().setVisible(true);
                }
            });
            Thread.sleep(2000);

            return null;
        }

        @Override
        protected void done() {
            System.out.println("now in done...");
            JDialog dialog = getDialog();
            // Don't care, dismiss the dialog
            dialog.setVisible(false);
        }

    }

    protected JDialog getDialog() {
        if (dialog == null) {

            dialog = new JDialog();
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dialog.setLayout(new BorderLayout());
            dialog.add(new JLabel("Please wait..."));
            dialog.setSize(200, 200);
            dialog.setLocationRelativeTo(null);

        }

        return dialog;
    }

}

这篇关于安全地打开和关闭模态JDialog(使用SwingWorker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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