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