gpt4 book ai didi

java - NetBeans RCP - 如何在应用程序运行且可见时显示对话框

转载 作者:行者123 更新时间:2023-11-30 04:19:14 25 4
gpt4 key购买 nike

我正在 NetBeans Framework (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);
}

最佳答案

您可以使用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;
}
}

关于java - NetBeans RCP - 如何在应用程序运行且可见时显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17535783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com