1

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);)?

lmiv
  • 315
  • 5
  • 17

1 Answers1

1

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.

Enigo
  • 3,685
  • 5
  • 29
  • 54
  • Thanks for the answer bro it worked. Gonna have a look at creating a new class. – lmiv Mar 04 '18 at 23:04
  • Yeah, cool) check this [answer](https://stackoverflow.com/a/29734804/5151575), looks like that is quite what you need. – Enigo Mar 05 '18 at 08:06