0

I went through some documentations here and here and found that gluon dialogs Can* have a generic type which will be the generic type of the object to be returned when you call showAndWait(). But gluon alerts (com.gluonhq.charm.glisten.control.Alert which is a subclass of com.gluonhq.charm.glisten.control.Dialog) does not seem to have a generic type and does not also seem to allow you to give it a generic type.

The problem occured when I tried to call setOnHidden as:

boolean shown;
String report = "";
Alert al = new Alert(AlertType.ERROR);
al.setContentText(report);
al.setAutoHide(false);
al.setOnHidden(e->{
    shown = false;
});
shown = true;
al.showAndWait();

and I got the following warning on the setOnHidden() Call:

The method setOnHidden(EventHandler) belongs to the raw type Dialog. References to generic type Dialog should be parameterized

Any clarifications about gluon dialogs or ways to get rid of the warning are most welcome.

SDIDSA
  • 894
  • 10
  • 19
  • Can you show your code? It'd also be helpful if you provided a link to the documentation you're referencing. – Slaw Oct 03 '18 at 19:38
  • i updated the post. – SDIDSA Oct 03 '18 at 19:43
  • 1
    By the way, if you can't avoid the warning and you're sure you know what you're doing, you can use [`@SuppressWarnings("unchecked")`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/SuppressWarnings.html). See [What is SuppressWarnings (“unchecked”) in Java?](https://stackoverflow.com/questions/1129795/what-is-suppresswarnings-unchecked-in-java?rq=1) and [What is the list of valid @SuppressWarnings warning names in Java?](https://stackoverflow.com/questions/1205995/what-is-the-list-of-valid-suppresswarnings-warning-names-in-java?rq=1) – Slaw Oct 03 '18 at 20:16
  • the code i provided in my question works and suppressing is sometimes useful, but i was trying to [work out a way of avoiding the warning rather than suppressing it](https://stackoverflow.com/a/1129812/8556269) – SDIDSA Oct 03 '18 at 20:20
  • 1
    I know it's better to always at least try to change the code first so the warning isn't emitted in the first place, but it may not be possible in this case due to how Gluon wrote the API (see [my comment](https://stackoverflow.com/questions/52634150/what-do-gluon-alerts-return-in-showandwait#comment92202995_52634819))—hence the "_...if you can't avoid the warning..._" bit. I was just providing the `SuppressWarnings` option in case you were unaware of said annotation. – Slaw Oct 03 '18 at 20:24
  • yeah it seems that there is no way of avoiding the warning yet, thank you for your help – SDIDSA Oct 03 '18 at 20:25
  • 2
    The issue will be fixed in the next Gluon Mobile release. – José Pereda Oct 03 '18 at 20:45

1 Answers1

1

Like in the built-in JavaFX Alert control, the implicit type of the Gluon's Alert control is the same: the JavaFX built-in ButtonType, so if you click the OK button, it will return ButtonType.OK.

As you can see at the Alert JavaDoc, the control has one or two default buttons: an OK button for all of them, and a Cancel button for the Confirmation alert. Each of these buttons has as default result ButtonType.OK and ButtonType.CANCEL.

So this works for both Alert controls:

alert.showAndWait().ifPresent(result -> {
   if (result == ButtonType.OK) {
       // do something;
   }
});

One of the things you will notice with both OK and Cancel buttons: the alert will be dismissed, so you don't have to do it.

You can also provide your custom buttons. Then you'll need to take care of calling hide():

final Button myYesButton = new Button("Yes");
myYesButton.setOnAction(event -> {
    alert.setResult(ButtonType.YES);
    alert.hide();
});
alert.getButtons().add(myYesButton);

About the setOnHidden, see Javadoc. It requires a LifecycleEvent:

alert.setOnHidden((LifecycleEvent event) -> System.out.println("alert hidden"));

but you can use just:

alert.setOnHidden(event -> System.out.println("alert hidden"));

Finally, make sure you are importing the right control:

import com.gluonhq.charm.glisten.control.Alert;

...
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR);
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • 1
    I think the problem is the signature of Gluon's `Alert`. The Gluon `Alert` extends Gluon's `Dialog` but doesn't specify a generic type (Gluon's `Dialog` has a generic parameter). However, if you look at the JavaFX `Dialog` and `Alert` you'll see that `Alert` extends `Dialog`. This seems like a bug in the Gluon API? – Slaw Oct 03 '18 at 20:05
  • 2
    Yes, that it seems, I'll file an issue. Thanks. – José Pereda Oct 03 '18 at 20:06