Search code examples
javaspringvaadinvaadin8

Vaadin Click Shortcut expropriation


I'm facing ClickShortcut problem with Vaadin 8. Please take look at my simple example code:

private Component shortcutTest()
{
    TabSheet tabSheet = new TabSheet();
    tabSheet.addTab( createButton( "button1", 
                                  ShortcutAction.KeyCode.ENTER, 
                                  ShortcutAction.ModifierKey.CTRL ),
                                  "Tab1" );
    tabSheet.addTab( createButton( "button2", 
                                  ShortcutAction.KeyCode.ENTER, 
                                  ShortcutAction.ModifierKey.CTRL ), 
                                  "Tab2" );
    return tabSheet;
}

private Component createButton(String name, int keyCode, int modifier)
{
    Button button = new Button( name );
    button.setClickShortcut( keyCode, modifier );
    button.addClickListener( event -> System.out.println( event.getButton().getCaption() + " - click!" ) );
    return button;
}

When selected tab is Tab1 Im getting expected output on Ctrl+Enter action, but when I select Tab2 - nothing happens.

How can I deal with that? I would like to have the same shortcut for different button in different tab, but it seems like I can have only one button for one shortcut combination.


Solution

  • Yes, looking into the framework code it looks like the same shortcut can only be used once per window (or the whole UI if you have no windows).

    One workaround could be to dynamically set and clear the shortcuts whenever the selected tab changes, something like

    tabSheet.addSelectedTabChangeListener(
            (TabSheet.SelectedTabChangeListener) event -> {
                if (tabsheet.getSelectedTab().equals(tab1)) {
                    button2.removeClickShortcut();
                    button1.setClickShortcut(keyCode, modifier);
                } else {
                    button1.removeClickShortcut();
                    button2.setClickShortcut(keyCode, modifier);
                }
            });