How to find which button is clicked in this dialog box and also get choice options .
I need a output if click Debug
and Ok
get choice bok select value and which button is clicked. And i have ref this enter link description here and change my code in
Optional<ButtonType> optional = choice.showAndWait();
if (optional.isPresent()) {
System.out.println(optional.get().getButtonData());
}
I will get this error
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to javafx.scene.control.ButtonType
at choicedialogfx.ChoiceDialogFX.choiceInputDialog(ChoiceDialogFX.java:74)
at choicedialogfx.ChoiceDialogFX$1.handle(ChoiceDialogFX.java:43)
at choicedialogfx.ChoiceDialogFX$1.handle(ChoiceDialogFX.java:39)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
This my sample code
public class ChoiceDialogFX extends Application {
@Override
public void start(Stage primaryStage) {
List<String> list = new ArrayList<>();
list.add("/dev/ttyUSB0");
list.add("/dev/ttyS0");
list.add("/dev/ttyS1");
list.add("/dev/ttyS2");
Button btn = new Button();
btn.setText("ChoiceDialog");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
choiceInputDialog("Test", "Port Select", "Choice", list);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public String choiceInputDialog(String title, String headerText, String contentText, List choices) {
Object defaultChoice = choices.get(0);
Collection<String> collection = choices;
ChoiceDialog choice = new ChoiceDialog(defaultChoice, collection);
addCheckBox(choice);
choice.setTitle(title);
choice.setHeaderText(headerText);
choice.setContentText(contentText);
Optional<ButtonType> optional = choice.showAndWait();
if (optional.isPresent()) {
System.out.println(optional.get());
}
return null;
}
private void addCheckBox(ChoiceDialog choice) {
ButtonType buttonType = new ButtonType("Debug", ButtonData.HELP);
choice.getDialogPane().getButtonTypes().add(buttonType);
}
}
If you have specified the exact type for ChoiceDialog you would have less problems while coding. Instead of just
do
Here we set the type
T
toString
, whereT
isIn other words, we want to show string options to user and return a string when user clicks on one of buttons. Note that it will be null for "Cancel".
Next. You want to return 2 values from dialog,
1. Value that user selected from combobox
2. The type of button the user clicked
Since we are returning only one string value, the naive solution will be to return merged values of 2 above, and split them on client code. More robust solutions will be setting a type-safe object to type T of ChoiceDialog above, or set the custom content for Dialog ourselves (See Custom Login Dialog example).
Naive solution:
EDIT:
Actually you can get the item selected by
so you may prefer to return the text of the button clicked only. Something like
where