Find which button is clicked in choice Dialog in j

2019-08-12 13:58发布

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

enter image description here

1条回答
再贱就再见
2楼-- · 2019-08-12 14:32

If you have specified the exact type for ChoiceDialog you would have less problems while coding. Instead of just

ChoiceDialog choice = new ChoiceDialog( defaultChoice, collection );

do

ChoiceDialog<String> choice = new ChoiceDialog( defaultChoice, collection );

Here we set the type T to String, where T is

T - The type of the items to show to the user, and the type that is returned via ChoiceDialog.getResult() when the dialog is dismissed.

In 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:

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

EDIT:

Actually you can get the item selected by

choice.getSelectedItem();

so you may prefer to return the text of the button clicked only. Something like

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

where

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;
    }
} );
查看更多
登录 后发表回答