JavaFX + Scene Builder how switch scene - javafx

I'm working with JavaFx and Scenebuilder and want create a local app for myself called "Taskplanner" in eclipse.
I created a new Stage and set it with a Scene (see Main.java). But not sure how to set a new Scene in the old stage (see Controller.java). Didnt also not find out if it is possible pass the signInButtonClicked()-Methode the "Stage primaryStage" over Scene Builder
Can anybody help ?
Controller.java:
#FXML
Button btnSignIn;
#FXML
public void signInButtonClicked() throws Exception
{
//Here I want call the new Scene(SignInGUI.fxml) in my old Stage
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/SignInGUI.fxml"));
}
Main.java:
#Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("../view/LoginGUI.fxml"));
primaryStage.setTitle("Taskplanner");
primaryStage.setScene(new Scene(root,500,500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

You can get a reference to the Scene and Window from your button reference. From there, it's up to you to decide how to you want to show the new view.
Here's how you get those references:
Scene scene = btnSignIn.getScene();
Window window = scene.getWindow();
Stage stage = (Stage) window;
You can change the view by changing the root of your Scene:
FXMLLoader loader = ... // create and load() view
btnSignIn.getScene().setRoot(loader.getRoot());
Or you can change the entire Scene:
FXMLLoader loader = ... // create and load() view
Stage stage = (Stage) btnSignIn.getScene().getWindow();
Scene scene = new Scene(loader.getRoot());
stage.setScene(scene);

Related

Opening fxml file for certain time and go back to the main screen?

How do I display an fxml file for some seconds and close that fxml file and return to the mainpage.fxml?
I have tried to add hideThispage method to the initialize method in the controller class of the fxml but I guess the initialize methods just runs before the class is actually loaded so I cannot run this code.
#FXML
public void hideThisPage() throws InterruptedException, IOException {
Window window = new Stage();
PauseTransition pause = new PauseTransition(Duration.seconds(5));
pause.setOnFinished(e -> window.hide());
pause.play();
FxmlDisplay fxmlDisplay = new FxmlDisplay();
fxmlDisplay.stageSelection("View/Main.fxml");
}
To return to the main.fxml or any fxml I have a method that I call:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(path));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
In my case, I have a print button in my printreceipt.fxml file that prints the contents. Then it switches to the printpage.fxml ( i am able to switch to the page) But my problem is the printpage.fxml is supposed to be displayed only for 5 seconds and close. then it should go back to main.fxml. how and where do I call the hidethisPage() method?
#FXML
void printRandomTicket(ActionEvent event) throws SQLException, ClassNotFoundException, JRException, IOException, InterruptedException {
FxmlDisplay fxmlDisplay = new FxmlDisplay();
((Node) event.getSource()).getScene().getWindow().hide();
fxmlDisplay.stageSelection("/View/Dialogs/PrintingTicket.fxml");
GenerateTicket generateTicket = new GenerateTicket();
generateTicket.generateTicket();
PrintTicket printTicket = new PrintTicket();
printTicket.printTicket();
}
I hope this clarifies it.

How to create own window rather then use default window

I've been working on javafx and i want to remove default windows and create a window with my style
It's quite easy to create a window in javafx. To create your own window you need to modify the style of your stage which can be done using initStyle() method.
public class Test extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createParent(), Color.TRANSPARENT);
primaryStage.initStyle(StageStyle.TRANSPARENT);
// primaryStage.initStyle(StageStyle.UNDECORATED);
// primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.setTitle("My Own Window");
primaryStage.setScene(scene);
primaryStage.show();
}
private Parent createParent() {
Pane rootPane = new Pane();
rootPane.setPrefSize(1000,400);
Button btn = new Button("RandomButton");
btn.setOnAction(e -> Platform.exit());
rootPane.getChildren().add(btn);
return rootPane;
}
}

How to close a new popup window from Controller in JavaFX

I have Controller.java for Scene.fxml and ControllerSettings.java for WindowSettings.fxml. In Controller.java I create a new popup window (no dialog) with following method:
#FXML
public void handleSubmenuSettings(ActionEvent event) throws IOException {
Stage stage;
Parent root;
ControllerSettings controller;
stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("WindowSettings.fxml"));
root = (Parent) loader.load();
controller = (ControllerSettings) loader.getController();
stage.setScene(new Scene(root));
stage.setTitle("Settings");
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
stage.initOwner(submenuSettings.getScene().getWindow());
stage.showAndWait();
stage.setOnCloseRequest(e -> {
e.consume();
controller.saveSettings();
stage.close();
});
}
I want to save the settings when closing the new popup window but that doesn't work with stage.setOnCloseRequest.
The showAndWait() method will block execution until the window has closed; i.e. subsequent statements will not be executed until after the window is closed. So you don't register the listener for the close request until after the window has been closed. Clearly, no request to close the window will occur after the window is closed, so your handler is never invoked.
It's not really clear why you are using a close request handler anyway. Presumably you simply want to call controller.saveSettings() after the window closes. Since you are using showAndWait() you can just do:
#FXML
public void handleSubmenuSettings(ActionEvent event) throws IOException {
Stage stage;
Parent root;
ControllerSettings controller;
stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("WindowSettings.fxml"));
root = (Parent) loader.load();
controller = (ControllerSettings) loader.getController();
stage.setScene(new Scene(root));
stage.setTitle("Settings");
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
stage.initOwner(submenuSettings.getScene().getWindow());
stage.showAndWait();
controller.saveSettings();
}

Opening a new scene in java FX

I have two FXML files and one Controller.
I have posted the code in which i have tried to create a second stage (and failed).
Error message is:
javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
How do I fix it?
#FXML private Scene trial_scene;
#FXML public Stage m;
#FXML public void click(Stage stage) throws IOException {
m = new Stage();
openWindow();
}
#FXML private void openWindow() throws IOException {
FXMLLoader root =FXMLLoader.load(
SampleController.class.getResource(
"settings.fxml"
)
);
m.initModality(Modality.WINDOW_MODAL);
m.setTitle("My modal window");
m.setScene(trial_scene);
m.show();
}
The static method FXMLLoader.load returns the root node (and so the objet hierarchy) defined in the .fxml file. In your case it seems to be an AnchorPane.
Your fist line should be
AnchorPane root = FXMLLoader.load(SampleController.class.getResource("settings.fxml"));
Use this object to construct your new scene, for example
Scene trial_scene = new Scene(root, 300, 300, Color.BLACK);
Then pass this scene to your new stage
m.setScene(trial_scene);

How to create a modal window in JavaFX 2.1

I can't figure out how to create a modal window in JavaFX. Basically I have file chooser and I want to ask the user a question when they select a file. I need this information in order to parse the file, so the execution needs to wait for the answer.
I've seen this question but I've not been able to find out how to implement this behavior.
In my opinion this is not good solution, because parent window is all time active.
For example if You want open window as modal after click button...
private void clickShow(ActionEvent event) {
Stage stage = new Stage();
Parent root = FXMLLoader.load(
YourClassController.class.getResource("YourClass.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("My modal window");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(
((Node)event.getSource()).getScene().getWindow() );
stage.show();
}
Now Your new window is REALY modal - parent is block.
also You can use
Modality.APPLICATION_MODAL
Here is link to a solution I created earlier for modal dialogs in JavaFX 2.1
The solution creates a modal stage on top of the current stage and takes action on the dialog results via event handlers for the dialog controls.
JavaFX 8+
The prior linked solution uses a dated event handler approach to take action after a dialog was dismissed. That approach was valid for pre-JavaFX 2.2 implementations. For JavaFX 8+ there is no need for event handers, instead, use the new Stage showAndWait() method. For example:
Stage dialog = new Stage();
// populate dialog with controls.
...
dialog.initOwner(parentStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();
// process result of dialog operation.
...
Note that, in order for things to work as expected, it is important to initialize the owner of the Stage and to initialize the modality of the Stage to either WINDOW_MODAL or APPLICATION_MODAL.
There are some high quality standard UI dialogs in JavaFX 8 and ControlsFX, if they fit your requirements, I advise using those rather than developing your own. Those in-built JavaFX Dialog and Alert classes also have initOwner and initModality and showAndWait methods, so that you can set the modality for them as you wish (note that, by default, the in-built dialogs are application modal).
You can create application like my sample. This is only single file JavaFX application.
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage stage;
stage = new Stage();
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(() -> {
try {
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
JasperDesign jasperDesign = JRXmlLoader.load(s + "/src/reports/report1.jrxml");
String query = "SELECT * FROM `accounttype`";
JRDesignQuery jrquery = new JRDesignQuery();
jrquery.setText(query);
jasperDesign.setQuery(jrquery);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint JasperPrint = JasperFillManager.fillReport(jasperReport, null, c);
//JRViewer viewer = new JRViewer(JasperPrint);
swingNode.setContent(new JRViewer(JasperPrint));
} catch (JRException ex) {
Logger.getLogger(AccountTypeController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Resources