Search code examples
javajavafxcursoralertscene

Changing the cursor in an alertbox JavaFX


Hi I am trying to change the cursor, in a JavaFX alert which displays once a button in sceneHome is pressed, once a buttontype is clicked.

This is the function thats gets called when the user presses the button in sceneHome:

public void export() {
    Alert alert = new Alert(AlertType.CONFIRMATION);
    alert.setTitle("Export menu");
    alert.setHeaderText("Wat wilt u exporteren, adressen of ritten?");
    alert.setContentText("Maak een keuze.");
    ButtonType buttonTypeOne = new ButtonType("Adressen");
    ButtonType buttonTypeTwo = new ButtonType("Ritten");
    ButtonType buttonTypeCancel = new ButtonType("Annuleren", ButtonData.CANCEL_CLOSE);

    alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeCancel);

    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == buttonTypeOne){
        //scene.setCursor(Cursor.WAIT);
        ToCSV.export("adressen");
        //scene.setCursor(Cursor.DEFAULT);
    } else if (result.get() == buttonTypeTwo) {
        //scene.setCursor(Cursor.WAIT);                
        ToCSV.export("ritten");
        //scene.setCursor(Cursor.DEFAULT);
    } else{
        //do nothing
    }
}

If I say sceneHome.setCursor(Cursor.WAIT) nothing happens and if I say alert.getDialogPane().getScene().setCursor(Cursor.WAIT) I get a NullPointerException...

So which scene should I pass in (at //scene.setCursor(Cursor.DEFAULT); and //scene.setCursor(Cursor.WAIT);)?


Solution

  • It looks like your ToCSV.export call actually block the program to change the cursor. Try to run execution of this method in separate Task:

    private static void export(Scene scene) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Export menu");
        alert.setHeaderText("Wat wilt u exporteren, adressen of ritten?");
        alert.setContentText("Maak een keuze.");
        ButtonType buttonTypeOne = new ButtonType("Adressen");
        ButtonType buttonTypeTwo = new ButtonType("Ritten");
        ButtonType buttonTypeCancel = new ButtonType("Annuleren", ButtonBar.ButtonData.CANCEL_CLOSE);
        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeCancel);
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeOne){
            scene.setCursor(Cursor.WAIT);
            final Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    ToCSV.export("adressen");
                    return null;
                }
                @Override
                protected void succeeded() {
                    scene.setCursor(Cursor.DEFAULT);
                }
            };
            new Thread(task).start();
        } else if (result.get() == buttonTypeTwo) {
            //
        } else{
            //do nothing
        }
    }
    

    The scene is the one where the button that opens this dialog located.

    Ideally, for the clean code sake, you should create a separate ToCSVTask class with this logic and submit it to ExecutorService - you may google the cleanest way how to do it.