This question already has an answer here:
JavaFX loading a new fxml file into the same scene
(1 answer)
Closed last year.
I want to reload my Scene of the Java Fx project.
So i created a button, which has an Fx-ID
now i want to create something, that reloads the whole fx scene after pressing the button...
How is this possible.?
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("!");
stage.setScene(scene);
stage
this is my scene
Try
Stage close = (Stage) restart.getScene().getWindow();
close.close();
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("!");
stage.setScene(scene);
stage.show();
}
this should help to close your stage. you will also create another stage, with deleted inputs
Related
I am learning JavaFx on my own and have not reached FXML yet. I am stuck at one application where I plan to have it go back to the main scene of the Application after the user enters their credentials on a second scene. I managed to bring up the second scene from the main one but I could not get from the second scene to the main one. I tried getting the main scene and pane using a getter but no luck. Can you guys teach the right way?
Thank you in advance.
public class Landing extends Application {
BorderPane bp;
Scene scene;
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Welcome to our Telco!");
bp = new BorderPane();
VBox vbox = new VBox();
Button login = new Button("Login");
login.setMinWidth(100);
Button acc = new Button("Account Information");
acc.setMinWidth(100);
vbox.getChildren().addAll(acc);
bp.setCenter(vbox);
acc.setOnAction(e ->{
AccountInfo account = new AccountInfo();
primaryStage.setTitle("Account Information"); // Set the stage title
primaryStage.getScene().setRoot(account.getbp());; // Place the scene in the stage
});
scene = new Scene(bp, 750, 550);
primaryStage.setScene(scene);
primaryStage.show();
}
public Pane getbp() {
return bp;
}
public Scene getSc(){
return scene;
}
the button to get the main scene
public class AccountInfo {
BorderPane pane;
Landing main = new Landing();
Scene scene;
AccountInfo() {
Button c = (new Button("Back"));
c.setStyle("-fx-background-color: pink");
c.setOnAction((ActionEvent e) -> {
main.getbp();
main.getSc();
});
public Pane getbp() {
return pane;
}
}
Landing is not a scene, it is an Application. So far from what you've shown, there is only one scene in your whole application. You must never try to instantiate (and subsequently run) more than one instance of any Application class within the same JavaFX application lifetime. You are dangerously going towards this direction when you do Landing main = new Landing(); in your AccountInfo class.
From Javadoc for Application.launch:
Throws: IllegalStateException - if this method is called more than
once.
What you need is to have the first scene for login (i.e. enter credentials). When login is successful, you create a new scene object and populate that scene with your next "view", then set that new scene to the stage.
public class Landing extends Application {
BorderPane bp;
Scene scene;
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Welcome to our Telco!");
bp = new BorderPane();
VBox vbox = new VBox();
Button login = new Button("Login");
login.setMinWidth(100);
Button acc = new Button("Account Information");
acc.setMinWidth(100);
vbox.getChildren().addAll(acc);
bp.setCenter(vbox);
acc.setOnAction(e -> {
primaryStage.setTitle("Account Information"); // Set the stage title
BorderPane infoScenePane = new BorderPane();
Scene infoScene = new Scene(infoScenePane);
primaryStage.setScene(infoScene);
});
scene = new Scene(bp, 750, 550);
primaryStage.setScene(scene);
primaryStage.show();
}
}
In a javafx application, I have my stage which contains an unique StackPanel.
In this StackPanel, I add the Panel I want to display (depending on what the user want).
Loading the StackPanel view :
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
stackLayout = (StackPane) loader.load();
Scene scene = new Scene(stackLayout);
stage.setScene(scene);
stage.show();
Adding a view to this StackPanel :
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MapGame.fxml"));
AnchorPane map = (AnchorPane) loader.load();
stackLayout.getChildren().clear();
stackLayout.getChildren().add(map);
What I want is to automatically resize my StackPane and Stage depending on the size of the child of my StackPane...
For example, my StackPane is 600x400, and the AnchorPane is 800x600, but when the Application is shown, the size is 600x400... Anybody ?
After these lines
stage.setScene(scene);
stage.show();
add the following:
stage.sizeToScene();
Edit: Try using this order in your code:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
stackLayout = (StackPane) loader.load();
loader.setLocation(Main.class.getResource("view/MapGame.fxml"));
AnchorPane map = (AnchorPane) loader.load();
stackLayout.getChildren().clear();
stackLayout.getChildren().add(map);
Scene scene = new Scene(stackLayout);
stage.setScene(scene);
stage.show();
stage.sizeToScene();
My app, if I click the "maximize button", it will maximize the window. But if I go to another scene(in the same stage), the window will restore to the original size. So, how can I control it to keep maximizing?
I used primaryStage.setMaximized(true); after calling show(); method in Java 8 implementation. It keeps other scenes maximizing.
I had the same problem. I fixed it creating a root stage with that only contains a menu bar and a tool bar, like this:
I initialized this window in the start method with:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
RootController controller= loader.getController();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
Later, when you want to load a FXML file or scene into the root container, you could make it with the next lines in another method:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/principal.fxml"));
AnchorPane principalOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(principalOverview);
// Get the controller instance
controllerPrincipal = loader.getController();
in that way every scene you add to the root stage will get the size of the root.
This is how I did it:
#FXML
private Button goBtn;
Stage stage = (Stage) goBtn.getScene().getWindow();
Scene scene = goBtn.getScene();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Activity.fxml"));
scene.setRoot(loader.load());
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
I'm working on this simple application and trying to create a new stage/window when clicking on a button. I get this work when using a simple fxml-document with only one AnchorPane. In the scene I want to pop-up, there is a TabPane with two tabs. I've imported one fxml-document into each tab, and when I do that a new AnchorPane is created in each tab. So when I click the button in main scene nothing is happening. I really don't know how to solve this, does anybody have any ideas?
This is the code I'm using right now to show a new stage.
#FXML
private void showAddStage(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Add.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
} catch (IOException e) {}
}
How to create custom dialog with FXML in JavaFX?
In samples over the Net I see mostly something like this
#Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(
getClass().getResource( getClass().getSimpleName() + ".fxml" ));
Scene scene = new Scene(root);
i.e. FXML is loaded from within application start() and builds root node.
But what if I extend Stage? Where to load from FXML? In constructor? Or in initStyle()? Or in some other method?
You may use the below code in your main Class :
FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = (Parent)loader.load();
//Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
SampleController controller = (SampleController)loader.getController();
controller.setStageAndSetupListeners(stage);
After this in SampleController Make a function setStageAndSetupListeners(), which will accept your stage and now you use it easily.