Carregar fxml como processo em segundo plano - Javafx
Meu fxml inicial (digamoshome.fxml
) possui muitas funcionalidades, portanto, leva muito tempo para carregar completamente. Portanto, para evitar o intervalo de tempo entre o início do programa e o carregamento de fxml, apresentei mais um fxml (digamosloader.fxml
) com uma imagem gif que deve aparecer enquanto o fxml principal está sendo carregado. O problema é que a imagem gif no meu loader.fxml não está se movendo, pois o programa está pendurado até que o home.fxml seja carregado completamente. Para evitar isso, mudei o carregamento do home.fxml para um thread, como mostrado no código abaixo.
public class UATReportGeneration extends Application {
private static Stage mainStage;
@Override
public void start(Stage stage) {
Parent loaderRoot = null;
try {
loaderRoot = FXMLLoader.load(getClass().getResource("/uatreportgeneration/fxml/Loader.fxml"));
} catch (IOException ex) {
Logger.getLogger(UATReportGeneration.class.getName()).log(Level.SEVERE, null, ex);
}
Scene loadScene = new Scene(loaderRoot);
stage.setScene(loadScene);
stage.initStyle(StageStyle.UNDECORATED);
stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/uatreportgeneration/Images/logo.png")));
stage.show();
mainStage = new Stage(StageStyle.UNDECORATED);
mainStage.setTitle("Upgrade Analysis");
mainStage.getIcons().add(new Image(this.getClass().getResourceAsStream("/uatreportgeneration/Images/logo.png")));
setStage(mainStage);
new Thread(() -> {
Platform.runLater(() -> {
try {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource("/uatreportgeneration/fxml/Home.fxml"));
Scene scene = new Scene(root);
mainStage.setScene(scene);
mainStage.show();
stage.hide();
System.out.println("Stage showing");
// Get current screen of the stage
ObservableList<Screen> screens = Screen.getScreensForRectangle(new Rectangle2D(mainStage.getX(), mainStage.getY(), mainStage.getWidth(), mainStage.getHeight()));
// Change stage properties
Rectangle2D bounds = screens.get(0).getVisualBounds();
mainStage.setX(bounds.getMinX());
mainStage.setY(bounds.getMinY());
mainStage.setWidth(bounds.getWidth());
mainStage.setHeight(bounds.getHeight());
System.out.println("thread complete");
} catch (IOException ex) {
Logger.getLogger(UATReportGeneration.class.getName()).log(Level.SEVERE, null, ex);
}
});
}).start();
}
public static Stage getStage() {
return mainStage;
}
public static void setStage(Stage stage) {
mainStage = stage;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Mas depois desse código também meu programa está travado (a imagem gif não está se movendo). Se eu carregar o fxml fora doPlatform.runLater()
, Eu recebo a exceçãoNot on FX Thread
.
Eu também cansei de usarTask()
mas a partir disso a imagem gif está se movendo, mas o fxml não está carregando em segundo plano, se eu tentar carregar o fxml foraPlatform.runLater()
.
Alguém pode me ajudar e me dizer como posso corrigir o código para que meu fxml seja carregado em segundo plano sem atrapalhar o processo em primeiro plano.