FranAguiar
FranAguiar

Reputation: 647

JavaFX change scene after click on button

I have a java desktop app that check if a new version of certain files is available and ask the user to download it. When the user click yes the question remains visible and I wan't to show a message like "downloading, please wait". But I don't know how do that...

private void updateAvailable(Stage stage) {
    stage.setTitle("A new version is available. Update?");
    stage.initStyle(StageStyle.UNDECORATED);
    Label label = new Label(stage.getTitle());
    label.setStyle("-fx-font-size: 25");
    Button yesButton = new Button("YES");
    Button noButton = new Button("NO");

    HBox buttons = new HBox(10, yesButton, noButton);
    buttons.setAlignment(Pos.CENTER);
    yesButton.setStyle("-fx-font-weight: bold");
    yesButton.setOnAction(event -> downloadFiles());

    noButton.setStyle("-fx-font-weight: bold");
    noButton.setOnAction(event -> executeApp());

    VBox root = new VBox(label, buttons);
    root.setAlignment(Pos.CENTER);
    root.setSpacing(20);
    root.setPadding(new Insets(25));
    root.setStyle("-fx-border-color: lightblue");

    stage.setScene(new Scene(root));
    stage.show();
}


private void downloadFiles(){
    updaterService.backupCurrentVersion();
    updaterService.downloadNewestVersion();
}

I tried to create a new scene inside the download method (before the download starts) and hide the current, but didn't work, the new scene only blinks when the download is finished.

What would be the best aproach to do this? I think I'm missunderstanding how scenes works

Upvotes: 2

Views: 3455

Answers (2)

LastM4N
LastM4N

Reputation: 2240

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; 

public class Main extends Application {

    Stage window;
    Button button;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        window = primaryStage;
        window.setTitle("thenewboston");
        button = new Button("Click Me");

        button.setOnAction(e -> AlertBox.display("Title of Window", Downloading..."));

        StackPane layout = new StackPane();
        layout.getChildren().add(button);
        Scene scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();
    }


}

When you click the button it will display a new scene with the message that you want to show.I did it with the Alert class.

Upvotes: 0

Anastasios Vlasopoulos
Anastasios Vlasopoulos

Reputation: 1802

You can use an Information Dialog for this job. The JavaFX offers the Alert class for this purpose. Something like below may be helpful in your case:

displayMessage(){
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setTitle("Information Dialog");
    alert.setHeaderText(null);
    alert.setContentText("Downloading, please wait");

    alert.showAndWait();
}

The above method could be included in the download button handler.

You can find more information regarding the Alert class in Oracle's documentation.
The above code can be included

Upvotes: 2

Related Questions