2

I want to add a ClickListener to an Item of a MenuBar in Vaadin.

I know about the normal situation, which i got working:

MenuBar menubar = new MenuBar();
menubar.addItem("Item", new MenuBar.Command() {

                    @Override
                    public void menuSelected(MenuItem selectedItem) {
                        //Do sth. when item is clicked
                    }
                });

In my application, I'm working with MVP, so the code which should run, is in an other class than the code which is defining the menubar.

Is there a way to add a listener to a specific item in the menubar ?

hoedding
  • 257
  • 1
  • 4
  • 14

1 Answers1

2

When you add an item to your MenuBar the function addItem(String,Command) actually returns a MenuItem which can be used later. You can do this :

MenuItem select = menuBar.addItem("Select", null);

And in another context you can add a listener on that MenuItem like this:

select.setCommand(new Command() {

  @Override
  public void menuSelected(MenuItem selectedItem) {
    System.out.println("You clicked on "+selectedItem.getText());
  }
});
deltascience
  • 3,321
  • 5
  • 42
  • 71