Thursday, July 8, 2010

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:-


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(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(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(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.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories

0 comments: to “ Menus In Java Swings

Extension Factory Builder