Adding the banner
This recipe will create a method that we will use to add a banner in the content panel.
Getting ready
Place the banner image banner.png at the location \web\resources\images. You
can use your own image or get it from the code sample provided for this book on the Packt
Publishing website (www.packtpub.com).
How to do it…
- Create the method getBanner:
public ContentPanel getBanner()
{
ContentPanel bannerPanel = new ContentPanel();
bannerPanel.setHeaderVisible(false);
bannerPanel.add(new Image("resources/images/banner.png"));
Image("resources/images/banner.png"));
return bannerPanel;
}
- Call the method setTopComponent of the ContentPanel class in the following
constructor:
setTop Component(getBanner());
How it works…
The method getBanner() creates an instance bannerPanel of type ContentPanel. The
bannerPanel will just show the image from the location resources/images/banner.
png. That’s why, the header is made invisible by invoking setHeaderVisible(false).
Instance of the com.google.gwt.user.client.ui.Image class, which represents the
banner image, is added in the bannerPanel.
In the default constructor of the HomePage class, the method
setTopComponent(getBanner()) is called to set the image as the top component of the
content panel.
See also
- The Creating the home page layout class recipe
- The Adding menus recipe
- The Creating the left-hand sidebar recipe
- The Creating the right-hand sidebar recipe
- The Creating main content panel recipe
- The Creating the footer recipe
- The Using the HomePage instance in EntryPoint recipe
Adding menus
In this recipe, we will create a method getMenuBar that does the following:
- Creates a menu bar
- Creates menus
- Creates menu items
- Adds menu items in menus
- Adds menus in the menu bar
How to do it…
Write the method header pu blic MenuBar getMenuBar(), and do the following in the
method body. Finally, this method should be called in the constructor of the class HomePage
to add the menu bar in the application.
- Create an instance of MenuBar:
MenuBar menuBar=new MenuBar();
- Create instances of Menu:
Menu fileMenu=new Menu();
Menu reportsMenu=new Menu();
Menu helpMenu=new Menu();
- Create the menu items and add them in corresponding menus:
//Items for File menu
MenuItem productMenuItem=new MenuItem("Product");
fileMenu.add(productMenuItem);
MenuItem stockMenuItem=new MenuItem("Stock");
fileMenu.add(stockMenuItem);
MenuItem purchaseMenuItem=new MenuItem("Purchase");
fileMenu.add(purchaseMenuItem);
MenuItem salesMenuItem=new MenuItem("Sales");
fileMenu.add(salesMenuItem);
//Items for Reports menu
MenuItem productListMenuItem=new MenuItem("Product List");
reportsMenu.add(productListMenuItem);
MenuItem stockStatusMenuItem=new MenuItem("Stock Status");
reportsMenu.add(stockStatusMenuItem);
MenuItem purchaseDetailMenuItem=new MenuItem(
"Purchase Detail");
reportsMenu.add(purchaseDetailMenuItem);
MenuItem salesDetailMenuItem=new MenuItem("Sales Detail");
reportsMenu.add(salesDetailMenuItem);
//Items for Help menu
MenuItem aboutMenuItem=new MenuItem("About");
helpMenu.add(aboutMenuItem);
- Create the menu bar items:
MenuBarItem menuBarItemFile=new MenuBarItem("File",fileMenu);
MenuBarItem menuBarItemReports=
new MenuBarItem("Reports",reportsMenu);
MenuBarItem menuBarItemHelp=
new MenuBarItem("Help",helpMenu);
- Add the menu bar items in menu bar:
menuBar.add(menuBarItemFile);
menuBar.add(menuBarItemReports);
menuBar.add(menuBarItemHelp);
- Return the menu bar:
return menuBar;
How it works…
The menu bar containing all the required menus with menu items is created in the
following ways:
- MenuBar instance menuBar is created where the menu bar items will be added.
- Three menus are created for File, Reports, and Help.
- Menu items are created and added under corresponding menus.
- Three instances of MenuBarItem are created for the three menus.
- All of the menu bar items are added in the menu bar. Call this
method in the HomePage constructor by writing add(getMenuBar,
menuBarToolBarLayoutData).
See also
- The Creating the home page layout class recipe
- The Adding the banner recipe
- The Creating the left-hand sidebar recipe
- The Creating the right-hand sidebar recipe
- The Creating the main content panel recipe
- The Creating the footer recipe
- The Using the HomePage instance in EntryPoint recipe
GWT Articles & Books
- Google Web ToolKit(GWT)
- History Management in GWT
- GWT Books
- GWT Official Site