Pages

Tuesday, June 23, 2009

Simple Menubar in GWT

This is just a simple example of menubar for GWT beginners.
Code:

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;

public class Menubar extends MenuBar{
MenuBar menu = new MenuBar();//main menubar
public Menubar() {



MenuBar home = new MenuBar(true);// Home menu
MenuBar newbar = new MenuBar(true); // New Sub menu
home.addItem("New", newbar);//adding New sub menu to Home menu

/*
* Adding Sub New 1 menu to New sub menu
*/

newbar.addItem("Sub New1",new Command(){
public void execute() {
Window.alert("Sub New 1 clicked");// define action here
}
});

newbar.addItem("Sub New2",new Command(){
public void execute() {
Window.alert("Sub New 2 clicked");
}
});

/*
* Adding menu home,support etc to main menubar
*/
this.addItem("Home", home);

this.addItem("Support",new Command(){
public void execute() {
Window.alert("Support clicked");
}
});

this.addItem("Services",new Command(){
public void execute() {
Window.alert("Services clicked");
}
});

this.addItem("Contact",new Command(){
public void execute() {
Window.alert("Contact clicked");
}
});

this.addItem("Help",new Command(){
public void execute() {
Window.alert("Help clicked");
}
});
}
public MenuBar displayMenu(){
return this;
}
}

EntryPoint class:

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

/**
* Entry point classes define onModuleLoad().
*/
public class Index implements EntryPoint {

/**
* This is the entry point method.
*/
Menubar menu = new Menubar();
public void onModuleLoad() {
RootPanel.get().add(menu.displayMenu());
}
}

Output:

No comments:

Post a Comment