查找在javafx的选择对话框中单击的按钮

查找在javafx的选择对话框中单击的按钮,java,javafx,javafx-8,Java,Javafx,Javafx 8,如何查找在此对话框中单击的按钮,以及如何获取选择选项 如果单击Debug和Okget choice bok select value并单击哪个按钮,我需要一个输出。我引用了这个,并在中更改了我的代码 Optional<ButtonType> optional = choice.showAndWait(); if (optional.isPresent()) { System.out.println(optional.get().getButtonDa

如何查找在此对话框中单击的按钮,以及如何获取选择选项

如果单击
Debug
Ok
get choice bok select value并单击哪个按钮,我需要一个输出。我引用了这个,并在中更改了我的代码

    Optional<ButtonType> optional = choice.showAndWait();
    if (optional.isPresent()) { 
        System.out.println(optional.get().getButtonData());

    } 
这是我的示例代码

    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);
    }
}
公共类ChoiceDialogFX扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
列表=新的ArrayList();
添加(“/dev/ttyUSB0”);
添加(“/dev/ttyS0”);
添加(“/dev/ttyS1”);
添加(“/dev/ttyS2”);
按钮btn=新按钮();
btn.setText(“精选对话”);

btn.setOnAction(新的EventHandler

如果您已经为ChoiceDialog指定了确切的类型,那么您在编码时会遇到更少的问题。而不仅仅是

ChoiceDialog choice = new ChoiceDialog( defaultChoice, collection );

编辑:

实际上,您可以通过

choice.getSelectedItem();
因此,您可能更喜欢只返回单击按钮的文本

Optional<String> result = choice.showAndWait();
if ( result.isPresent() )
{
    System.out.println( "button text = " + result.get() );
    System.out.println( "choice = " + choice.getSelectedItem());
} 

如何在“替换调试”按钮中添加复选框。@ReeganMiranda设置对话框的自定义内容(请参阅)。
public String choiceInputDialog( String title, String headerText, String contentText, List choices )
{
    ChoiceDialog<String> choice = new ChoiceDialog( choices.get( 0 ), choices );
    addCheckBox( choice );

    choice.setResultConverter( ( ButtonType type ) ->
    {
        ButtonBar.ButtonData data = type == null ? null : type.getButtonData();
        if ( data == ButtonBar.ButtonData.HELP )
        {
            return "DEBUG@" + choice.getSelectedItem();
        }
        else if ( data == ButtonBar.ButtonData.OK_DONE )
        {
            return "OK@" + choice.getSelectedItem();
        }
        else
        {
            return null;
        }
    } );

    choice.setTitle( title );
    choice.setHeaderText( headerText );
    choice.setContentText( contentText );
    Optional<String> result = choice.showAndWait();
    if ( result.isPresent() )
    {
        System.out.println( "result: " + Arrays.toString( result.get().split( "@" ) ) );
    }
    return null;
}
choice.getSelectedItem();
Optional<String> result = choice.showAndWait();
if ( result.isPresent() )
{
    System.out.println( "button text = " + result.get() );
    System.out.println( "choice = " + choice.getSelectedItem());
} 
choice.setResultConverter( ( ButtonType type ) ->
{
    ButtonBar.ButtonData data = type == null ? null : type.getButtonData();
    if ( data == ButtonBar.ButtonData.HELP )
    {
        return "DEBUG";
    }
    else if ( data == ButtonBar.ButtonData.OK_DONE )
    {
        return "OK";
    }
    else
    {
        return null;
    }
} );