如何在Vaadin确认对话框中访问按钮元素?

huangapple go评论61阅读模式
英文:

How to access button elements within Vaadin confirm dialog?

问题

I want to set the focus on one of the buttons manually. What is the proper way to access these from within the Java class. I cannot see the button when I do

getElement().getChildren()...

When I inspect the dialog with the browser developer tools I can see the Element right there:

Cancel

Thanks and Best Regards

Matt

英文:

I want to set the focus on one of the buttons manually. What is the proper way to access these from within the Java class. I cannot see the button when I do

getElement().getChildren()...

When I inspect the dialog with the browser developer tools I can see the Element right there:

<vaadin-button theme="tertiary" slot="cancel-button" tabindex="0" role="button">Cancel</vaadin-button>

Thanks and Best Regads

Matt

答案1

得分: 2

你需要在那里使用自定义按钮。请参考下面的示例。我创建了两个按钮,另一个是“是”按钮。我在附加时以编程方式将焦点设置为它(即对话框已打开)。当您按下回车键时,将触发“是”按钮,而不是“否”按钮。

@Override
public void afterNavigation(AfterNavigationEvent event) {
    Button confirmButton = new Button("是");
    confirmButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
    Button noButton = new Button("否");
    noButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
    ConfirmDialog dialog = new ConfirmDialog();
    dialog.add("确认");
    dialog.setCancelButton(confirmButton);
    dialog.setCancelable(true);
    dialog.setConfirmButton(confirmButton);
    dialog.addConfirmListener(e -> Notification.show("已确认"));
    dialog.addCancelListener(e -> Notification.show("已取消"));
    dialog.open();
    confirmButton.addAttachListener(e -> confirmButton.focus());
}
英文:

You need to use custom buttons there. See the example below. I create two buttons, the other being "Yes" button. I programmatically focus that when attached (i.e. Dialog has been opened). When you hit enter key, that will trigger "Yes" button, not the "No" button.

@Override
public void afterNavigation(AfterNavigationEvent event) {
    Button confirmButton = new Button("Yes");
    confirmButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
    Button noButton = new Button("No");
    noButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
    ConfirmDialog dialog = new ConfirmDialog();
    dialog.add("Confirm me");
    dialog.setCancelButton(confirmButton);
    dialog.setCancelable(true);
    dialog.setConfirmButton(confirmButton);
    dialog.addConfirmListener(e -> Notification.show("Confirmed"));
    dialog.addCancelListener(e -> Notification.show("Cancelled"));
    dialog.open();
    confirmButton.addAttachListener(e -> confirmButton.focus());
}

huangapple
  • 本文由 发表于 2023年5月10日 21:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定