JavaFX - Appelé depuis une sous-classe, Setter de Stage primaryStage ne fonctionne pas
Bonjour,
J'ai un problème dont je pense que vous pourriez m'aider.
Pour aller droit au but, je vais déjà publier un peu de code.
Master Class
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public class MASTERCLASS extends Application {
private screenLoader screenLoader;
private static screenMenu screenMenu;
private static Stage window;
@Override
public void start(Stage primaryStage) throws Exception{
setWindow(primaryStage);
setScreenLoader(new screenLoader());
setScreenMenu(new screenMenu());
/** AFFICHAGE DE LA FENETRE **/
getScreenLoader().displayWindow();
[...]
}
public Parent loadingContent(String url) {
try {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource(url));
return (root);
}
catch (IOException e) {
e.printStackTrace();
return (null);
}
}
public Scene createContent(Parent root) {
Scene scene = new Scene(root);
String css = getClass().getResource("../css/defaultStyle.css").toExternalForm();
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
return (scene);
}
public static Stage getWindow() { return window; }
public static void setWindow(Stage window) { MuffinAttack.window = window; }
public screenLoader getScreenLoader() { return screenLoader; }
public void setScreenLoader(screenLoader screenLoader) { this.screenLoader = screenLoader; }
public static screenMenu getScreenMenu() { return screenMenu; }
public static void setScreenMenu(screenMenu screenMenu) { MuffinAttack.screenMenu = screenMenu; }
} |
Fichier contenant ScreenLoader
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
public class screenLoader extends MASTERCLASS {
private Line loaderBar = new Line();
private Line loaderBar2 = new Line();
private ResourceLoadingTask task = new ResourceLoadingTask();
private int textWidth = 200;
private Parent loadingContent() {
Pane root = new Pane();
root.setPrefSize(800, 600);
Text copyright = new Text();
copyright.setFont(new Font(12));
copyright.setTextAlignment(TextAlignment.CENTER);
copyright.setWrappingWidth(textWidth);
copyright.setX((800 / 2) - (textWidth / 2));
copyright.setY(600 - 5);
copyright.setText("Copyright BLA BLA BLA");
LoadingCircle loadingCircle = new LoadingCircle();
loadingCircle.setTranslateX(800 - 120);
loadingCircle.setTranslateY(600 - 100);
Rectangle loadingBarBG = new Rectangle(100, 600 - 70, 800 - 200, 5);
loadingBarBG.setFill(Color.DARKGREY);
loadingBarBG.setStroke(Color.BLUE);
loaderBar.setStartX(100);
loaderBar.setStartY(600 - 70);
loaderBar.setEndX(100);
loaderBar.setEndY(600 - 70);
loaderBar.setStroke(Color.AZURE);
loaderBar2.setStartX(100);
loaderBar2.setStartY(600 - 65);
loaderBar2.setEndX(100);
loaderBar2.setEndY(600 - 65);
loaderBar2.setStroke(Color.AZURE);
task.progressProperty().addListener((observable, oldValue, newValue) -> {
double progress = newValue.doubleValue();
loaderBar.setEndX(100 + progress * (800 - 200));
loaderBar2.setEndX(100 + progress * (800 - 200));
});
root.getChildren().addAll(copyright, loadingCircle, loadingBarBG, loaderBar, loaderBar2);
return (root);
}
private RotateTransition animation;
class LoadingCircle extends Parent {
private LoadingCircle() {
Circle circle = new Circle(20);
circle.setFill(null);
circle.setStroke(Color.WHITE);
circle.setStrokeWidth(5);
Rectangle rect = new Rectangle(25, 25);
Shape shape = Shape.subtract(circle, rect);
shape.setFill(Color.DARKGREY);
shape.setStroke(Color.DARKGREY);
getChildren().add(shape);
animation = new RotateTransition(Duration.seconds(2.5), this);
animation.setByAngle(360);
animation.setInterpolator(Interpolator.LINEAR);
animation.setCycleCount(Animation.INDEFINITE);
animation.play();
}
}
class ResourceLoadingTask extends Task<Void> {
@Override
protected Void call() throws Exception {
for (int i = 0; i < 100; i++) {
Thread.sleep((int)(Math.random() * 100));
updateProgress(i + 1, 100);
}
animation.stop();
// /!\
// C'EST ICI QUE JE VEUX DE L'AIDE
getScreenMenu().displayWindow(); // AUCUNE ERREUR REMARQUÉE PAR L'IDE, RIEN NE S'AFFICHE
// /!\
return (null);
}
}
public void displayWindow() throws InterruptedException {
System.out.println("YOLO I AM TEST - YOUR FATHER!");
getWindow().setTitle("JE SUIS : Loader");
getWindow().setScene(createContent(this.loadingContent()));
getWindow().setResizable(false);
getWindow().sizeToScene();
getWindow().show();
Thread t = new Thread(task);
t.start();
}
} |
Fichier contenant ScreenMenu
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public class screenMenu extends MASTERCLASS {
public void displayWindow() {
getWindow().setTitle("JE SUIS - Menu");
getWindow().setScene(createContent(loadingContent("../_view/viewMenu.fxml")));
getWindow().setResizable(false);
getWindow().sizeToScene();
getWindow().show();
}
} |
Voilà en gros mon code...
Je n'arrive pas à changer de Scene.
Et lorsque j'instancie dans la sous-classe, je ne peux pas utiliser le Setter de getWindow().setTitle("..."), [...].setScene(data), etc.
Merci de bien vouloir m'aider...