Question :
I have a Stage
Principal that is my Login:
In%ofthisstageIhaveaFXML
andaRegion
withtheProgressIndicator
property
AftersuccessfullyloggingintotheapplicationtheintentionwouldbetodisplaythissetVisible(false)
andRegion
whileathreadstartedtheapplication,butwhathappensisthatafterloggingin/em>startsrunningbyloadingtheapplication,butProgressIndicator
andRegion
arenotvisible.
Mythread
Taskt=newTask(){@OverrideprotectedObjectcall()throwsException{Platform.runLater(()->{try{newSisgeFX().start();}catch(IOExceptionex){Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE,null,ex);}});returnnull;}};region.visibleProperty().bind(t.runningProperty());pi.visibleProperty().bind(t.runningProperty());Threadth=newThread(t);th.start();
Itrieddoingtwothreads:onetoloadthesystemandanothertoupdateProgressIndicator
whilethesystemisnotloaded,buttonoavail,doesnotgenerateProgressIndicator
,I’vetrieditinseveralways.
WhatInoticedinmymanyattempts:
Threaddoesnotstart.
Excerptfromlogin:
@FXMLprivatevoidsysLogin(){Stringuser=ctfUserLogin.getText();Stringpass=ctfPassLogin.getText();LoginDAOloginDAO=DAOFactory.make(LoginDAO.class);Loginlogin=loginDAO.getLogin(user,pass);if(login!=null){runThread();//aquichamoaThreadpostadoacima.ctfPassLogin.setStyle(null);ctfUserLogin.setStyle(null);}else{ctfPassLogin.clear();ctfUserLogin.clear();ctfPassLogin.setStyle("-fx-border-color:red;");
ctfUserLogin.setStyle("-fx-border-color:red;");
//new ShakeTransition(vBox).play();
new WobbleTransition(vBox).play();
//new TadaTransition(vBox).play();
}
}
After successfully logging in to Exception
while the system loads in the background?
Answer :
An elegant way to solve your problem is to create a Preloader, considered a good practice programming in JavaFX. Preloader is especially useful for improving the user experience when it needs to wait for heavy operations to complete before the application starts. There is even a way to make a Preloader Login, explained in detail in this Oracle tutorial – Section 9.3.5 .
However, in the examples above, loading is done before login and not later. (Maybe there is a workaround to achieve this effect)
(NetBeans) Creating a Prealoder:
Within the init () method, you can load all the load required by your application, punctuating the progress using the notifyPreloader(new ProgressNotification(0.10)) // Para 10%;
Here is an example of a preloader with a simple progress bar:
public class AppPreloader extends Preloader{
private Stage stage;
private ProgressBar bar;
private boolean noLoadingProgress = true;
private Scene createPreloaderScene() {
bar = new ProgressBar();
VBox vb = new VBox();
vb.setAlignment(Pos.CENTER);
bar.setPrefWidth(150);
vb.getChildren().addAll(bar);
return new Scene(vb, 300, 150);
}
@Override
public void start(Stage stage){
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
@Override
public void handleStateChangeNotification(StateChangeNotification scn) {
}
@Override
public void handleProgressNotification(ProgressNotification pn) {
//application loading progress is rescaled to be first 50%
//Even if there is nothing to load 0% and 100% events can be
// delivered
if (pn.getProgress() != 1.0 || !noLoadingProgress) {
bar.setProgress(pn.getProgress()/2);
if (pn.getProgress() > 0) {
noLoadingProgress = false;
}
}
}
@Override
public void handleApplicationNotification(PreloaderNotification pn) {
if (pn instanceof ProgressNotification) {
//expect application to send us progress notifications
//with progress ranging from 0 to 1.0
double v = ((ProgressNotification) pn).getProgress();
if (!noLoadingProgress) {
//if we were receiving loading progress notifications
//then progress is already at 50%.
//Rescale application progress to start from 50%
v = 0.5 + v/2;
}
bar.setProgress(v);
} else if (pn instanceof StateChangeNotification) {
//hide after get any state update from application
stage.hide();
}
}
}
I did not see this in your code:
progressBar.progressProperty().bind(task.progressProperty());
This line will make the progressbar run.