#A question for JavaFX
11 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @cloud tide! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
So i am switching between windows
like clicking a button and the stage will chane
i watched a tut about that and person did a code like this to prevent opening another stage
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
why doing this? i still couldnt understand it
the code you show is just getting the stage from which an event originated.
it does nothing to prevent opening a new stage
u can do that, but u can also be a lot neater about it, encapsulate it properly and apply proper single responsonsibility principles too
if u created a javaFX project, u should have something that looks similar to this:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader fxml = new FXMLLoader(ClassLoader.getSystemResource("layout.fxml"));
//Create buffer class to improve MVC compliance
fxml.setController(new MVCSplitter());
Parent root = fxml.load();
//Build and Display Stage
primaryStage.setTitle("calculator");
primaryStage.setScene(new Scene(root)); //, 400, 275));
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
(in this code i dont swap scenes so i dont need the stage more than once)
what u do instead is something like this:
public class SceneSwapper {
private final Stage stage;
public SceneSwapper(Stage stage) {
...
}
public swapScene(Scene newScene) {
...
stage.setStage(newScene);
}
public static Scene loadScene(String filename) {
URL sceneLocation = ClassLoader.getSystemResource("layout.fxml");
if (sceneLocation == null)
throw new ...Exception("Nice explanation about missing fmxl file");
try {
Parent root = FXMLLoader.load(sceneLocation);
return new Scene(root);
catch (IOException e) {
//log error
throw new ....Exception("Cannot read file: \"\{ filename }\"");
}
}
public class Main extends Application {
private SceneSwapper sceneSwapper;
@Override
public void start(Stage primaryStage) { //note that u handled the checked exception in method designed for loading the scenes so u dont need `throws` anymore
sceneSwapper = new SceneSwapper(primaryStage);
Scene nextScene = SceneSwapper.loadScene(filename);
sceneSwapper.swapScene(nextScene);
}
public static void main(String[] args) { launch(args); }
}
if ur going to do something in code more than once, write methods to do it cleanly and clearly, dont duplicate code, dont make classes do things they arent supposed to (follow the single responsibility principle)
... the controller for ur login scene should be responsible for controlling ur login scene NOT for trying to swap scenes on the stage