TextField f = new TextField();
Button b = new Button("Save");
b.setClickShortcut(KeyCode.ENTER); // For quick saving from text field itself
TextArea longText = new TextArea(); // "Enter" is garbled here
热门使快捷方式仅在from文本字段中起作用?
答案 0 :(得分:9)
使用focus和blur listeners删除并添加快捷键:
f.addFocusListener(new FocusListener() {
@Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
@Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
答案 1 :(得分:1)
较新版本的Vaadin需要以下代码,因为现在不推荐使用addListener()。
f.addFocusListener(new FocusListener() {
private static final long serialVersionUID = -6733373447805994139L;
@Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
private static final long serialVersionUID = -3673311830300629513L;
@Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
答案 2 :(得分:0)
谈论Vaadin 14 我一直在寻找答案,对我来说,这很好用
Button search = new Button("Search");
search.addClickShortcut(Key.ENTER);