1

我正在 NetBeans 框架(RCP)上开发一个 Java 应用程序。当应用程序运行并可见时,我想显示一个“请稍候”对话框,该对话框将禁止用户使用 GUI,直到正在运行的线程完成。请看下面我的代码。它现在的工作方式是在应用程序可见之前显示“请稍候”对话框。再一次,我想在应用程序可见时显示“请稍候”对话框,而不是之前。我该怎么做呢?

public final class MyTopComponent extends TopComponent {

    public CoreTopComponent() {
        initComponents();
    }

    @Override
    public void componentOpened() {

            //Show a Dialog ontop of the application with a Please Wait message so that the user cannot use the application until the following threads
            //are complete. The Dialog is supposed to automatically close once the threads are complete.

            JOptionPane.showOptionDialog(this, "Please Wait...",
            "Please Wait...", JOptionPane.PLAIN_MESSAGE,
            JOptionPane.PLAIN_MESSAGE, null, new Object[] {},
            null);


            //There are 10 threads like the following 2 that perform something and in the end update the GUI

            final Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    //Step 1 - Create class that initializes configuration

                    //Step 2 - Create class that performs something using configuration

                    //Step 3 - Update GUI on the EDT using SwingUtilities.invokeLater
                }
            });
            t1.start();

            final Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    //Step 1 - Create class that initializes configuration

                    //Step 2 - Create class that performs something using configuration

                    //Step 3 - Update GUI on the EDT using SwingUtilities.invokeLater
                }
            });
            t2.start(); 
    }


    @Override
    public void componentClosed() {

    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}

我尝试使用以下内容,但仍然发生相同的情况:

@Override
public void componentShowing() {
    JOptionPane.showOptionDialog(this, "Please Wait...",
            "Please Wait...", JOptionPane.PLAIN_MESSAGE,
            JOptionPane.PLAIN_MESSAGE, null, new Object[]{},
            null);
}
4

2 回答 2

1

您可以使用ProgressUtils .showProgressDialogAndRun() 系列方法在 UI 中心显示进度条,该进度条会阻止 UI 直到您的任务完成。

实现ProgressRunnable接口将使您的任务可以访问ProgressHandle。然后,使用该showProgressDialogAndRun(ProgressRunnable<T> operation, String displayName, boolean includeDetailLabel)方法,您可以为需要运行的每个子任务设置详细标签。

这是一个片段,显示了您可以执行此操作的不同方式。此场景中的主要任务是“运行启动”任务。它驱动所有子任务,将 ProgressHandle 传递给每个子任务,以便每个子任务都可以设置详细标签:

    ...
    final List<ProgressRunnable> tasks = new ArrayList<ProgressRunnable>();
    tasks.add(new ProgressRunnable<Object>() {
        @Override
        public Object run(ProgressHandle ph) {
            ph.progress("Work unit 1", 1);
            return null;
        }
    });
    tasks.add(new CancellableTask());
    ProgressUtils.showProgressDialogAndRun(new ProgressRunnable<Void>() {
        @Override
        public Void run(ProgressHandle ph) {
            // run all tasks passing in the ProgressHandle to each
            for (ProgressRunnable task : tasks) {
                task.run(ph);
            }
            return null;
        }
    }, "Running Startup", true);
    ...
    // will show a Cancel button on the progress UI
    private class CancellableTask implements ProgressRunnable<Object>, Cancellable {

    @Override
    public Object run(ProgressHandle ph) {
        ph.progress("Cancellable work unit", 2);
        return null;
    }

    @Override
    public boolean cancel() {
        // clean up 
        return true;
    }
}
于 2013-07-13T17:41:13.660 回答
1

JOptionPane 创建模态 JDialogs。为了避免这种情况,您可以通过创建线程使其成为非模态:

    Thread waitThread = new Thread(new Runnable(){
        public void run(){
            JOptionPane.showOptionDialog(rootPane, "Please Wait...",
                "Please Wait...", JOptionPane.PLAIN_MESSAGE,
                JOptionPane.PLAIN_MESSAGE, null, new Object[] {},
                null);
    }

通过以下方式调用它:

waitThread.start();

请注意,我将父组件更改为rootPane而不是null,但您可以根据应用程序的需要执行此操作。这将解决您的问题并允许其余代码在对话框启动时运行。

确保在其他线程之前调用它,以便它们可以在此对话框启动时运行。

这是一个类似的问题:

在不停止执行流程的情况下显示“JOptionPane.showMessageDialog”

于 2013-07-08T21:16:34.147 回答