[英]Find which button is clicked in choice Dialog in javafx
如何在此对话框中找到单击的按钮,并获取选项选项。
我需要一个输出,如果单击Debug
和Ok
获取选择bok选择值和单击哪个按钮。 我在这里输入了这个输入链接描述并更改了我的代码
Optional<ButtonType> optional = choice.showAndWait();
if (optional.isPresent()) {
System.out.println(optional.get().getButtonData());
}
我会得到这个错误
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)
这是我的示例代码
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);
}
}
如果您已为ChoiceDialog指定了确切的类型,则编码时遇到的问题会更少。 而不仅仅是
ChoiceDialog choice = new ChoiceDialog( defaultChoice, collection );
做
ChoiceDialog<String> choice = new ChoiceDialog( defaultChoice, collection );
这里我们将类型T
设置为String
,其中T
为
T - 要向用户显示的项的类型,以及在解除对话框时通过ChoiceDialog.getResult()返回的类型。
换句话说,我们希望向用户显示字符串选项,并在用户单击其中一个按钮时返回字符串。 请注意,对于“取消”,它将为null。
下一个。 您想从对话框中返回2个值,
1.用户从组合框中选择的值
2.用户点击的按钮类型
由于我们只返回一个字符串值,因此天真的解决方案将返回上面的合并值2,并将它们拆分为客户端代码。 更强大的解决方案是将类型安全对象设置为上面的Type of ChoiceDialog,或者自己设置Dialog的自定义内容(请参阅自定义登录对话框示例 )。
天真的解决方案:
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;
}
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.