Saturday, May 30, 2009

Menus In Java Swings

Every window can have a menu associated with it.Menu Bars basic function is to provide a list of choices.Each choice has its own drop-down menu(is provided otherwise none).This is implemented in Java by using following classes:-

MenuBar
Menu
MenuItem
CheckboxMenuItem

A menu bar can contains many menu objects.Each menu object may contain many MenuItem objects.In this implementation it is also possible to include checkable menu items.These can be implemented by CheckboxMenuItem class and will place a check mark next to them when they get selected.

First look at the constructors involved in creation of a menubar application.

1. MenuBar:-MenuBar is the highest class in this application and it will contain all the menu's inside it.It has only default constructor.

//MenuBar () only default constructor

2.Menu:-The constructors for Menu is as following.Menu's object will hold menu item's in them.

//Menu()
//Menu(String optionName)
//Menu(String optionName,boolean Removable)

In this case optionName provides the name of menu selection.If removable are set to true then menu can be removed and allowed to roam free.Otherwise it will remain with the menu bar.

3.MenuItem:-The Individual menuitems are of type MenuItem .These controls are to be placed on Menu object.

//MenuItem()
//MenuItem(String itemName)
//MenuItem(String itemName,MenuShortcut key)

here itemName is the name of item shown on menu and key is the menu shortcut for this item.

4.CheckboxMenuItem:-CheckboxMenuItem creates the checkboxes which can be placed as a menu and show their state by checked or unchecked.

//CheckboxMenuItem()
//CheckboxMenuItem(String itemName)
//CheckboxMenuItem(String itemName,boolean on)

itemName refers to the name of checkbox displayed on menu and on refers its state checked or unchecked.

Important Methods:-There are methods with various functionality lets see

1.void setEnabled(boolean flagvalue):-This method sets any menuitem to enabled or disabled state.Which specifies whether the menuitem is active or not(Click works or not).

2.boolean isEnabled():-This method returns the value whether the menuitem is active or inactive.That is either true or false.

3.void setLabel(String newName):-This method changes the name of a menuitem.

4.String getLabel():-It returns the name of the menuitem.

5.boolean getState():-This method returns the value (checked or unchecked) of
CheckboxMenuItem object.

6.void setState(boolean checked):-You can set the value of CheckboxMenuItem to true or false using this method.

For learning an example click below:-

How Menu is created step wise step procedure

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP