Events and Listeners

Hello,

I am new to Vaadin and i am having a hard time trying to get a MenuBar to fire an event that should be listened by another class.

My application basically has two layouts: one is the menu, which contains a menubar, and the other is the main application body.

What I would like to do is to fire an event every time a menubar item is clicked, making it possible to listen to this event from another class to update the application body with a new layout… Is this approach correct? How do i implement it?

I can’t find much information about events on the forum and vaadin’s site.

Thanks in advance,

Diogo

Hi! I think the events and listeners are such a basic topic that they get ignored by most of the people here. And actually the listener in MenuBar is called Command. Here is a small code sample to show you the idea and get you started:


// Create a body layout
final Layout body = new VerticalLayout();
mainWindow.addComponent(body);

// Create a menubar w/ two commands
MenuBar mb = new MenuBar();
mb.addItem("item one", new MenuBar.Command() {
		
	public void menuSelected(MenuItem selectedItem) {
			body.removeAllComponents();
			body.addComponent(new YourCustomComponent("one"));
		}
	});
mb.addItem("item two", new MenuBar.Command() {
	
	public void menuSelected(MenuItem selectedItem) {
			body.removeAllComponents();
			body.addComponent(new YourCustomComponent("two"));
		}
	});
mainWindow.addComponent(mb);

/* ... some other code perhaps ...  */

/* Simple custom component for body content */
public class YourCustomComponent extends CustomComponent {
	
	public YourCustomComponent() {
		setCompositionRoot(new Label("Placeholder Label"));
	}
}

Hi:

What if I don’t have the MenuBar in the aplication class?.

What I have being doing so far is have the listeners in the application class…how can I do that with the Command?

I apply the listener to the diferent classes using, for example:

 addListener((Property.ValueChangeListener) app);

regards,

Allready solved this!!!..thanks anyway…