gwt

Create MenuBar Example

This is an example of how to create a MenuBar using the Google Web Toolkit, that is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. Creating a MenuBar implies that you should:

  • The MenuExample class implements the com.google.gwt.core.client.EntryPoint interface to allow the class to act as a module entry point. It overrides its onModuleLoad() method. It also implements the com.google.gwt.user.client.Command so that it can have a Command associated with it that it executes when it is chosen by the user.
  • Create a new Instance of MenuBar.
  • Create sub- MenuBar items and add Items to them.
  • Add the sub- MenuBar items to the MenuBar.
  • Add the MenuBarto the RootPanel, that is the panel to which all other widgets must ultimately be added.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.enterprise;

import com.google.gwt.user.client.Command;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;

public class MenuExample implements EntryPoint, Command {

	@Override
	public void onModuleLoad() {
		//Create new Instance of MenuBar
		MenuBar menu = new MenuBar();
		
		menu.setAutoOpen(true);

		//Set subMenu Items for Item 2 of Menu0
		MenuBar subMenu = new MenuBar(true);
		subMenu.addItem("Item 0,2,0", true, this);
		subMenu.addItem("Item 0,2,1", true, this);
		subMenu.addItem("Item 0,2,2", true, this);

		//Set Menu0 Items
		MenuBar menu0 = new MenuBar(true);
		menu0.addItem("Item 0,0", true, this);
		menu0.addItem("Item 0,1", true, this);
		menu0.addItem("Item 0,2", true, subMenu);
		
		//Set Menu1 Items
		MenuBar menu1 = new MenuBar(true);
		menu1.addItem("Item 1,0", true, this);
		menu1.addItem("Item 1,1", true, this);
		menu1.addItem("Item 1,2", true, this);
		
		//Set Menu2 Items
		MenuBar menu2 = new MenuBar(true);
		menu2.addItem("Item 2,0", this);
		menu2.addItem("Item 2,1", this);
		menu2.addItem("Item 2,2", true, this);

		//Add Menu Items to Menu
		menu.addItem(new MenuItem("Menu 0", menu0));
		menu.addItem(new MenuItem("Menu 1", menu1));
		menu.addItem(new MenuItem("Menu 2", menu2));

		menu.setWidth("100%");

		//Add Menu to Root Panel
		RootPanel.get().add(menu);
	}

	@Override
	public void execute() {
		return;
	}

	
}

 
This was an example of how to create a MenuBar with the Google Web Toolkit.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button