I don't know how to close the main window in javafxml - java

I don't know how to close the main window in java fxml.
This part of code is in the class Main:
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent root2 = FXMLLoader.load(getClass().getResource("2ndwin.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
Scene scene2 = new Scene(root2);
secondaryStage.setScene(scene2);
}
public void show(){
secondaryStage.show();
}
I've got this. In controller i did this:
Main m = new Main();
m.show();`
but I still don't know how I can close primaryStage.
Please help me or tell me how I can create a new window and close the old window. I think this what I want to do - it isn't correct but I came up with it myself.

I do it by using an object in the stage you want to close to get the window
Window currentStage = OBJECTINSCENE.getScene().getWindow();
(Replace 'OBJECTINSCENE' with the id of anything in your scene). This gives you a reference to the stage you have open. Then call
currentStage.hide();
To close the stage when you want to.
So your show function would be as follows
public void show(){
Window currentStage = OBJECTINSCENE.getScene().getWindow();
secondaryStage.show();
currentStage.hide();
}

Related

JavaFX - How to change scene using a scene from another class?

I have the following class which has a button.
public class GUI extends Application {
private BorderPane mainLayout = new BorderPane();
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Main Menu");
FlowPane layout = new FlowPane();
Button button = new Button("Click");
layout.getChildren().addAll(button);
mainLayout.setTop(layout);
Scene scene = new Scene(mainLayout, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
If I have another class with a scene, how can I update the GUI class to show the scene by pressing the button?
The preferred mechanism would probably be to get the stage dynamically from a trigger event, for example:
button.setOnAction(event -> {
Scene newScene = // ... commands which define the new scene.
Stage stage = ((Node) event.getTarget()).getScene().getStage();
// or alternatively, just:
// Stage stage = button.getScene().getStage();
stage.setScene(newScene);
});
An alternative is to provide a static accessor to the main stage in the Application.
Change your GUI class to add an accessor for the stage:
public class GUI extends Application {
private static Stage guiStage;
public static Stage getStage() {
return guiStage;
}
#Override
public void start(Stage primaryStage) {
guiStage = primaryStage;
// other app initialization logic . . .
}
}
In your class which needs to change the scene for the GUI stage to a new scene, invoke:
Scene newScene = // ... commands which define the new scene.
GUI.getStage().setScene(newScene);
Using a static accessor in this specific instance is generally OK, because you can only have a single Application instance launched for a given JVM execution. The only real drawback is that you have a coded dependency between the class creating your new scene and your Application class. But, for some application types, this won't matter.

JavaFX getIcon()

I've been trying to figure out how to position my Stage Icon beside my Stage title. I was unable to formulate a solution for this. How do I get my Icon to be position beside my title?
stage.getIcons().add(new Image(""));
stage.setTitle("");
stage.setScene(scene);
stage.show();
I don't have 50 reputation to add a comment, but you don't really explain what the problem is with your current code. Those are the right methods, but maybe your file locations just aren't right? See the following example:
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(root);
stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/images/so-icon.png")));
stage.setTitle("Icon Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

How could i disable primary stage when a new stage popup

I have button that will popup new stage but primary stage wont disable it still can be click so how to disable the primary stage.
This is my code for stage and scene
private static final Stage stage = new Stage();
private static final Stage newstage = new Stage();
/**
*
*/
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().addAll(this.getClass().getResource("background.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
public void chgScene (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void addStage (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
newstage.setScene(scene);
newstage.setResizable(false);
newstage.showAndWait();
}
public void clearStage () {
newstage.close();
}
If any mistaken so sorry since im just new to Java and i need to create a GUI for my project. Thanks for helping.
you need to add this:
newstage.initModality(Modality.APPLICATION_MODAL);
newstage.showAndWait();
see Modality
JavaFx has a various property that you can use, just it depends on the developer how he/she creative on his logic.
mainPane.setDisabled(true);
newPane.showAndWait();
Be advised that initModality() is broken and has been for a long time on the Linux platform runtime.
modal JavaFX stage initOwner prevents owner from resizing, bug?
Yes, that post directly refers to initOwner() but it's the call to initModality() that seems to trigger the bug. You can waste a lot of time playing with kludges that approximate the behavior you need but nothing seems to work just right.

Can't create new stage with multiple AnchorPanes

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 return to code after I'm done with JavaFX scene

I'm launching my JavaFX scene as:
Applicaiton.launch(Main.class);
form my java code.
How to return to my code after I'm done with JavaFX!
Example:
public String Method()
{
Stirng s = "MyName";
Application.launch(Main.class);//here I lauch JavaFX scene
s.trim();//how to come back here after I'm done with that scene.
}
It's not supposed to work that way. You start by extending javafx.application.Application, then the entry point is start(Stage), which you must override. That method is the place where you have to set up the Scene for your stage, build the layout with Node's (buttons, layout managers, text fields, checkboxes), and register event handlers. You can access startup parameters with getParameters().
The application can be launched by providing the usual main() that calls launch(), a facility of JavaFX. So the minimal JavaFX application looks like:
public class MyApp extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
root.getChildren().setAll(new Label("Hello world!"));
Scene scene = new Scene(root, 600, 400);
// Add widgets and set up event handlers
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args) {
launch(args);
}
}
Platform.exit(); will do it click here for javafx2.2 documentation

Categories