给定JavaFX应用程序
public class App extends Application{
@Override
public void init() throws Exception {
// Do some huge task
}
@Override
public void start(Stage primaryStage) {
// even a bigger task
}
}
和Preloader
类
public class PreloadScreen extends Preloader {
// all the overwritten methods
@Override
public void handleStateChangeNotification(StateChangeNotification info) {
// Handle state change notifications.
StateChangeNotification.Type type = info.getType();
if(type.equals(BEFORE_START))
// hide the preloader
}
}
据我所知,预加载器隐藏在应用程序的start()
之前。将此图像视为可视化
是否可以将StateChangeNotification
添加到枚举中,如BEFORE_SHOW
我有一个非常密集的任务,需要应用程序中start方法的阶段。
答案 0 :(得分:3)
定义自定义进度消息:
public class ProgressMessage implements Preloader.PreloaderNotification
{
public static final ProgressMessage SUCCESSFULLY_DONE = new ProgressMessage(true, false);
public static final ProgressMessage FAILED = new ProgressMessage(false, true);
// TODO: add a variable to hold the exception in case of failure.
private double progress;
private String message;
private boolean done;
private boolean failed;
public ProgressMessage(double progress, String message)
{
this.progress = progress;
this.message = message;
this.done = false;
this.failed = false;
}
private ProgressMessage(boolean done, boolean failed)
{
this.done = done;
this.failed = failed;
}
public double getProgress(){return progress;}
public String getMessage(){return message;}
public boolean isDone(){return done;}
public boolean isFailed(){return failed;}
@Override
public boolean equals(Object o)
{
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
ProgressMessage message1 = (ProgressMessage) o;
return Double.compare(message1.progress, progress) == 0 &&
done == message1.done && failed == message1.failed &&
Objects.equals(message, message1.message);
}
@Override
public int hashCode()
{
return Objects.hash(progress, message, done, failed);
}
@Override
public String toString()
{
return "ProgressMessage{" + "progress=" + progress + ", message='" +
message + '\'' + ", done=" + done + ", failed=" + failed + '}';
}
}
在Preloader中,覆盖handleApplicationNotification(PreloaderNotification info)
:
@Override
public void handleApplicationNotification(PreloaderNotification info)
{
ProgressMessage progressMessage = (ProgressMessage) info;
if(progressMessage.isDone())
{
splashScreenStage.hide();
LOGGER.info("The splash screen is closed");
}
else if(progressMessage.isFailed())
{
// TODO: receive the exception from the main application.
// TODO: show error dialog here with exception details.
LOGGER.error("Exiting the application due to an error during the startup!");
splashScreenStage.hide();
Platform.exit();
}
else
{
double progress = progressMessage.getProgress();
String message = progressMessage.getMessage();
LOGGER.info("New Progress: progress({}), message({})", progress, message);
splashScreenController.setProgress(progress);
splashScreenController.setProgressMessage(message);
}
}
现在,您可以从主应用程序调用以下方法来通知进度:
notifyPreloader(new ProgressMessage(0.5, "Downloading some data from the backend server")); // 0.5 = 50%
notifyPreloader(ProgressMessage.SUCCESSFULLY_DONE);
notifyPreloader(ProgressMessage.FAILED);