JavaFX fade out stage and close - java

I'm having difficulty finding out if this is even possible. The common behavior that most people want is to fade out an extension of a node, which is entirely possible through a FadeTransition
However, I'm trying to fade out an entire stage, so imagine closing the running program and instead of simply killing the windows (i.e. showing -> not showing), I would like the window (stage) to fade out over 2 seconds like a toast or notification would.

Create a timeline with a KeyFrame that changes the opacity of the stage's scene's root node. Also make sure to set the Stage style and the scene fill to transparent. Then make the program exit once the timeline is finished.
Below is an application with a single big button that, when clicked, will take 2 seconds to fade away, and then the program will close.
public class StageFadeExample extends Application {
#Override
public void start(Stage arg0) throws Exception {
Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT); //Removes window decorations
Button close = new Button("Fade away");
close.setOnAction((actionEvent) -> {
Timeline timeline = new Timeline();
KeyFrame key = new KeyFrame(Duration.millis(2000),
new KeyValue (stage.getScene().getRoot().opacityProperty(), 0));
timeline.getKeyFrames().add(key);
timeline.setOnFinished((ae) -> System.exit(1));
timeline.play();
});
Scene scene = new Scene(close, 300, 300);
scene.setFill(Color.TRANSPARENT); //Makes scene background transparent
stage.setScene(scene);
stage.show();
}
public static void main (String[] args) {
launch();
}
}

Related

JavaFX BorderPane inside an AnchorPane?

I'm currently trying to show a BorderPane inside my AnchorPane (that is also inside my StackPane). I tried to show a new Scene but it show me 2 items in my Windows taskbar, and this is not really what I want.
For exemple, when you open the Preference in Eclipse, u get another window that is not visible in the taskbar.
So basically, the only thing I need is a window that looks like an ImageView, like anchored in the AnchorPane , not resizable and undecorated.
Expected result at end
This is what I tried:
btnDice.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent arg0) {
Scene newScene = new Scene(new BorderPane(), 230, 100);
Stage newWindow = new Stage();
newWindow.setScene(newScene);
newWindow.show();
}
});

How to block a main window in JavaFX

There is a button in a scene of the main window. When it's clicked, the new window is created, according following code:
public static void create()
{
Stage stage = new Stage();
AnchorPane pane = new AnchorPane();
//Here i add some into pane
stage.setScene(new Scene(pane));
stage.setWidth(500);
stage.setHeight(600);
stage.show();
}
I'd like the main window will remain blocked (i.e. an user won't be able to click on buttons, type text, resize or interact with it with other ways) until the additional window is closed.
Here is a link that shows exactly what you're looking for: http://www.javafxtutorials.com/tutorials/creating-a-pop-up-window-in-javafx/
Here is the main part of the code you need to add:
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(btn1.getScene().getWindow());
stage.showAndWait(); // This forces the program to pay attention ONLY to this popup window until its closed
Hope this helps.

Updating your UI and forcibly waiting before continuing JavaFX

I show here an image of my welcome scene.
The current behavior of the Create New Project button is shown here:
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
stage.hide();
Parent root = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Configure New Project Settings");
stage.setScene(scene);
stage.show();
To explain it in words: the button is pressed -> I get the stage -> hide the current scene -> switch the stage to have a new scene -> re-display the stage. This behavior is working.
I am new to GUI programming so please bear with me as I explain what I need.
I am now trying to add a small feature where when the Create New Project button is pressed, a loading message appears in the current scene, I force the GUI to wait for a few seconds (so that the user has time to see this "loading" message) before continuing onto the next scene. The loading message would look something like this.
I really want to implement this feature because a loading message followed by a wait could be more intuitive and consequently improve my users experience when using the program.
My initial attempt was to do the following:
statusText.setText("Please wait..."); // statusText is the "loading message"
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
stage.hide();
Parent root = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Configure New Project Settings");
stage.setScene(scene);
stage.show();
I just read here that you can never sleep the UI thread because it will freeze the entire application. So I've been trying the approach mentioned in the first answer from the above link, which says to use javafx.concurrent.Task, however I have been trying for a long time to no avail.
How do I update the UI, forcibly wait for a few seconds, and then display a new scene?
Instead of sleeping the thread, use a PauseTransition and load your new scene after the pause has finished.
createNewProject.setOnAction(event -> {
statusText.setText("Please wait...");
PauseTransition pause = new PauseTransition(
Duration.seconds(1),
);
pause.setOnFinished(event -> {
Parent root = FXMLLoader.load(
getClass().getResource(
"/view/scene/configure/NewProjectConfigureScene.fxml"
)
);
Scene scene = new Scene(root);
stage.setTitle("Configure New Project Settings");
stage.setScene(scene);
stage.sizeToScene();
});
pause.play();
});
The above code assumes you have just a single stage, so you resize your "Welcome" stage to become the "Configure New Project Settings" stage.
You should try adding in ControlsFX this will help.
You can do the following:
Service<T> service = new Service<T>(){
#Override
protected Task<T> createTask() {
return new Task<T>() {
#Override
protected T call() throws Exception {
//Do your processing here. }
};
}
};
service.setOnSucceeded(event -> {//do your processing});
service.setOnFailed(event -> {//do your processing});
ProgressDialog pd = new ProgressDialog(service);
pd.setContentText("Please wait while the window loads...");
pd.initModality(Modality.WINDOW_MODAL);
pd.initOwner(stage);
service.start();
This will put your code on the background thread. ControlsFX dialogs start and stop with the service.

How can i lock a screen in a certain position (javafx)?

i have to make a game for a school project and i was wondering if it's possible to lock a screen in a certain position on the screen. I have a settings button that opens a new stage and i want to make that scene immovable is this possible?
This is the code where i display the settings screen:
Public void buttonAction(){
btnSettings.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
GridPane settingsGrid = new GridPane();
Scene scene1 = new Scene(settingsGrid,375,600);
Stage settingsStage = new Stage();
settingsGrid.setHgap(10);
settingsGrid.setVgap(10);
//settingsGrid.setGridLinesVisible(true);
settingsStage.setTitle("Settings");
settingsStage.setResizable(false);
settingsStage.initStyle(StageStyle.UTILITY);
settingsStage.initModality(Modality.APPLICATION_MODAL);
settingsStage.setScene(scene1);
settingsStage.showAndWait();
}
});
}
Thanks in advance!
you could try this:
settingsStage.initStyle(StageStyle.TRANSPARENT);
However, you could only have one initStyle if I remember correctly. So you have to settingsStage.initStyle(StageStyle.UTILITY); in order to use the above code. What it does is it make the background transparent and the user could not change the position.

is it possible to open a new fxml file in the same frame

i've created fxml files in anchor pane
but everytime i click on the button i get the next fxml in a new frame
i want it to open in the same frame
public void baropen(ActionEvent event) {
// handle the event here
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(10, 50, 50, 50));
Stage stage = new Stage();
Scene scene ;
// scene= new Scene(root);
scene = new Scene(bp);
stage.setScene(scene);
stage.show();
try {
new RecBar().start(stage);
} catch (Exception ex) {
Logger.getLogger(RecController.class.getName()).log(Level.SEVERE, null,ex);
}
}
Just create a single Stage and when you want to replace the content of the stage with a new fxml, load the new fxml into a new Scene and call stage.setScene.
It's a theater metaphor - imagine you are watching a play - it's Romeo and Juliet, the curtain raises and you see the first scene (a plaza in Verona with a fountain). Later on, the curtain lowers, little men run around and change stuff, the curtain rises and you see a new scene (the balcony to Juliet's bedroom). The scene has changed, but the stage has not - there is just one stage and multiple scenes.

Categories