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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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