You are on page 1of 11

java.

awt
Class Menu
public class Menu
extends MenuItem
implements MenuContainer, Accessible
A Menu object is a pull-down menu component that is deployed from a menu bar.

Constructor Summary

Menu()
Constructs a new menu with an empty label.

Menu(String label)
Constructs a new menu with the specified label.

Menu(String label, boolean tearOff)


Constructs a new menu with the specified label, indicating whether the menu can be torn off.

Method Summary

MenuItem add(MenuItem mi)


Adds the specified menu item to this menu.

void add(String label)


Adds an item with the specified label to this menu.

void addNotify()
Creates the menu's peer.

void addSeparator()
Adds a separator line, or a hypen, to the menu at the current position.

int countItems()
Deprecated. As of JDK version 1.1, replaced by getItemCount().

AccessibleConte getAccessibleContext()
xt Gets the AccessibleContext associated with this Menu.

MenuItem getItem(int index)


Gets the item located at the specified index of this menu.

int getItemCount()
Get the number of items in this menu.

void insert(MenuItem menuitem, int index)


Inserts a menu item into this menu at the specified position.

void insert(String label, int index)


Inserts a menu item with the specified label into this menu at the specified position.

void insertSeparator(int index)


Inserts a separator at the specified position.

boolean isTearOff()
Indicates whether this menu is a tear-off menu.

String paramString()
Returns a string representing the state of this Menu.

void remove(int index)


Removes the menu item at the specified index from this menu.

void remove(MenuComponent item)


Removes the specified menu item from this menu.

void removeAll()
Removes all items from this menu.

void removeNotify()
Removes the menu's peer.

Constructor Detail

Menu
public Menu()
throws HeadlessException
Constructs a new menu with an empty label. This menu is not a tear-off menu.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.1
See Also:
GraphicsEnvironment.isHeadless()
!
Menu
public Menu(String label)
throws HeadlessException
Constructs a new menu with the specified label. This menu is not a tear-off menu.
Parameters:
label - the menu's label in the menu bar, or in another menu of which this menu is a submenu.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
See Also:
GraphicsEnvironment.isHeadless()
!
Menu
public Menu(String label,
boolean tearOff)
throws HeadlessException
Constructs a new menu with the specified label, indicating whether the menu can be torn off.
Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored.
Parameters:
label - the menu's label in the menu bar, or in another menu of which this menu is a submenu.
tearOff - if true, the menu is a tear-off menu.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.0.
See Also:
GraphicsEnvironment.isHeadless()

Method Detail

addNotify
public void addNotify()
Creates the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality.
Overrides:
addNotify in class MenuItem
!
removeNotify
public void removeNotify()
Removes the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality.
Overrides:
removeNotify in class MenuComponent
!
isTearOff
public boolean isTearOff()
Indicates whether this menu is a tear-off menu.
Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored.
Returns:
true if this is a tear-off menu; false otherwise.
!
getItemCount
public int getItemCount()
Get the number of items in this menu.
Returns:
the number of items in this menu.
Since:
JDK1.1
!
countItems
public int countItems()
Deprecated. As of JDK version 1.1, replaced by getItemCount().
!
getItem
public MenuItem getItem(int index)
Gets the item located at the specified index of this menu.
Parameters:
index - the position of the item to be returned.
Returns:
the item located at the specified index.
!
add
public MenuItem add(MenuItem mi)
Adds the specified menu item to this menu. If the menu item has been part of another menu, removes it from that menu.
Parameters:
mi - the menu item to be added
Returns:
the menu item added
See Also:
insert(java.lang.String, int), insert(java.awt.MenuItem, int)
!
add
public void add(String label)
Adds an item with the specified label to this menu.
Parameters:
label - the text on the item
See Also:
insert(java.lang.String, int), insert(java.awt.MenuItem, int)
!
insert
public void insert(MenuItem menuitem,
int index)
Inserts a menu item into this menu at the specified position.
Parameters:
menuitem - the menu item to be inserted.
index - the position at which the menu item should be inserted.
Throws:
IllegalArgumentException - if the value of index is less than zero
Since:
JDK1.1
See Also:
add(java.lang.String), add(java.awt.MenuItem)
!
insert
public void insert(String label,
int index)
Inserts a menu item with the specified label into this menu at the specified position. This is a convenience method for insert(menuItem, index).
Parameters:
label - the text on the item
index - the position at which the menu item should be inserted
Throws:
IllegalArgumentException - if the value of index is less than zero
Since:
JDK1.1
See Also:
add(java.lang.String), add(java.awt.MenuItem)
!
addSeparator
public void addSeparator()
Adds a separator line, or a hypen, to the menu at the current position.
See Also:
insertSeparator(int)
!
insertSeparator
public void insertSeparator(int index)
Inserts a separator at the specified position.
Parameters:
index - the position at which the menu separator should be inserted.
Throws:
IllegalArgumentException - if the value of index is less than 0.
Since:
JDK1.1
See Also:
addSeparator()
!
remove
public void remove(int index)
Removes the menu item at the specified index from this menu.
Parameters:
index - the position of the item to be removed.
!
remove
public void remove(MenuComponent item)
Removes the specified menu item from this menu.
Specified by:
remove in interface MenuContainer
Parameters:
item - the item to be removed from the menu. If item is null or is not in this menu, this method does nothing.
!
removeAll
public void removeAll()
Removes all items from this menu.
Since:
JDK1.0.
!
paramString
public String paramString()
Returns a string representing the state of this Menu. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary
between implementations. The returned string may be empty but may not be null.
Overrides:
paramString in class MenuItem
Returns:
the parameter string of this menu
!
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Menu. For menus, the AccessibleContext takes the form of an AccessibleAWTMenu. A new AccessibleAWTMenu instance is
created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class MenuItem
Returns:
an AccessibleAWTMenu that serves as the AccessibleContext of this Menu
!

java.awt
Class MenuBar
public class MenuBar
extends MenuComponent
implements MenuContainer, Accessible
The MenuBar class encapsulates the platform's concept of a menu bar bound to a frame. In order to associate the menu bar with a Frame object, call the frame's setMenuBar method.

Constructor Summary

MenuBar()
Creates a new menu bar.

Method Summary

Menu add(Menu m)
Adds the specified menu to the menu bar.

void addNotify()
Creates the menu bar's peer.

int countMenus()
Deprecated. As of JDK version 1.1, replaced by getMenuCount().

void deleteShortcut(MenuShortcut s)
Deletes the specified menu shortcut.

AccessibleConte getAccessibleContext()
xt Gets the AccessibleContext associated with this MenuBar.

Menu getHelpMenu()
Gets the help menu on the menu bar.

Menu getMenu(int i)
Gets the specified menu.

int getMenuCount()
Gets the number of menus on the menu bar.

MenuItem getShortcutMenuItem(MenuShortcut s)
Gets the instance of MenuItem associated with the specified MenuShortcut object, or null if none of the menu items being managed by this menu bar is
associated with the specified menu shortcut.

void remove(int index)


Removes the menu located at the specified index from this menu bar.

void remove(MenuComponent m)
Removes the specified menu component from this menu bar.

void removeNotify()
Removes the menu bar's peer.

void setHelpMenu(Menu m)
Sets the specified menu to be this menu bar's help menu.
Enumeration shortcuts()
Gets an enumeration of all menu shortcuts this menu bar is managing.

Constructor Detail

MenuBar
public MenuBar()
throws HeadlessException
Creates a new menu bar.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
See Also:
GraphicsEnvironment.isHeadless()

Method Detail

addNotify
public void addNotify()
Creates the menu bar's peer. The peer allows us to change the appearance of the menu bar without changing any of the menu bar's functionality.
!
removeNotify
public void removeNotify()
Removes the menu bar's peer. The peer allows us to change the appearance of the menu bar without changing any of the menu bar's functionality.
Overrides:
removeNotify in class MenuComponent
!
getHelpMenu
public Menu getHelpMenu()
Gets the help menu on the menu bar.
Returns:
the help menu on this menu bar.
!
setHelpMenu
public void setHelpMenu(Menu m)
Sets the specified menu to be this menu bar's help menu. If this menu bar has an existing help menu, the old help menu is removed from the menu bar, and replaced with the
specified menu.
Parameters:
m - the menu to be set as the help menu
!
add
public Menu add(Menu m)
Adds the specified menu to the menu bar.
Parameters:
m - the menu to be added.
Returns:
the menu added.
See Also:
remove(int), remove(java.awt.MenuComponent)
!
remove
public void remove(int index)
Removes the menu located at the specified index from this menu bar.
Parameters:
index - the position of the menu to be removed.
See Also:
add(java.awt.Menu)
!
remove
public void remove(MenuComponent m)
Removes the specified menu component from this menu bar.
Specified by:
remove in interface MenuContainer
Parameters:
m - the menu component to be removed.
See Also:
add(java.awt.Menu)
!
getMenuCount
public int getMenuCount()
Gets the number of menus on the menu bar.
Returns:
the number of menus on the menu bar.
Since:
JDK1.1
!
countMenus
public int countMenus()
Deprecated. As of JDK version 1.1, replaced by getMenuCount().
!
getMenu
public Menu getMenu(int i)
Gets the specified menu.
Parameters:
i - the index position of the menu to be returned.
Returns:
the menu at the specified index of this menu bar.
!
shortcuts
public Enumeration shortcuts()
Gets an enumeration of all menu shortcuts this menu bar is managing.
Returns:
an enumeration of menu shortcuts that this menu bar is managing.
Since:
JDK1.1
See Also:
MenuShortcut
!
getShortcutMenuItem
public MenuItem getShortcutMenuItem(MenuShortcut s)
Gets the instance of MenuItem associated with the specified MenuShortcut object, or null if none of the menu items being managed by this menu bar is associated with the
specified menu shortcut.
Parameters:
s - the specified menu shortcut.
Since:
JDK1.1
See Also:
MenuItem, MenuShortcut
!
deleteShortcut
public void deleteShortcut(MenuShortcut s)
Deletes the specified menu shortcut.
Parameters:
s - the menu shortcut to delete.
Since:
JDK1.1
!
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this MenuBar. For menu bars, the AccessibleContext takes the form of an AccessibleAWTMenuBar. A new AccessibleAWTMenuBar
instance is created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class MenuComponent
Returns:
an AccessibleAWTMenuBar that serves as the AccessibleContext of this MenuBar

java.awt
Class MenuItem
public class MenuItem
extends MenuComponent
implements Accessible
All items in a menu must belong to the class MenuItem, or one of its subclasses.

Constructor Summary

MenuItem()
Constructs a new MenuItem with an empty label and no keyboard shortcut.

MenuItem(String label)
Constructs a new MenuItem with the specified label and no keyboard shortcut.

MenuItem(String label, MenuShortcut s)


Create a menu item with an associated keyboard shortcut.

Method Summary

void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this menu item.

void addNotify()
Creates the menu item's peer.
void deleteShortcut()
Delete any MenuShortcut object associated with this menu item.

void disable()
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).

protected disableEvents(long eventsToDisable)


void Disables event delivery to this menu item for events defined by the specified event mask parameter.

void enable()
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).

void enable(boolean b)
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).

protected enableEvents(long eventsToEnable)


void Enables event delivery to this menu item for events to be defined by the specified event mask parameter

AccessibleConte getAccessibleContext()
xt Gets the AccessibleContext associated with this MenuItem.

String getActionCommand()
Gets the command name of the action event that is fired by this menu item.

ActionListener[ getActionListeners()
] Returns an array of all the action listeners registered on this menu item.

String getLabel()
Gets the label for this menu item.

EventListener[] getListeners(Class listenerType)


Returns an array of all the objects currently registered as FooListeners upon this MenuItem.

MenuShortcut getShortcut()
Get the MenuShortcut object associated with this menu item,

boolean isEnabled()
Checks whether this menu item is enabled.

String paramString()
Returns a string representing the state of this MenuItem.

protected processActionEvent(ActionEvent e)
void Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects.

protected processEvent(AWTEvent e)
void Processes events on this menu item.

void removeActionListener(ActionListener l)
Removes the specified action listener so it no longer receives action events from this menu item.

void setActionCommand(String command)


Sets the command name of the action event that is fired by this menu item.

void setEnabled(boolean b)
Sets whether or not this menu item can be chosen.

void setLabel(String label)


Sets the label for this menu item to the specified label.

void setShortcut(MenuShortcut s)
Set the MenuShortcut object associated with this menu item.

Constructor Detail

MenuItem
public MenuItem()
throws HeadlessException
Constructs a new MenuItem with an empty label and no keyboard shortcut.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.1
See Also:
GraphicsEnvironment.isHeadless()
!
MenuItem
public MenuItem(String label)
throws HeadlessException
Constructs a new MenuItem with the specified label and no keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all
menu items except for separators are enabled.
Parameters:
label - the label for this menu item.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.0
See Also:
GraphicsEnvironment.isHeadless()
!
MenuItem
public MenuItem(String label,
MenuShortcut s)
throws HeadlessException
Create a menu item with an associated keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except
for separators are enabled.
Parameters:
label - the label for this menu item.
s - the instance of MenuShortcut associated with this menu item.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.1
See Also:
GraphicsEnvironment.isHeadless()

Method Detail

addNotify
public void addNotify()
Creates the menu item's peer. The peer allows us to modify the appearance of the menu item without changing its functionality.
!
getLabel
public String getLabel()
Gets the label for this menu item.
Returns:
the label of this menu item, or null if this menu item has no label.
Since:
JDK1.0
See Also:
setLabel(java.lang.String)
!
setLabel
public void setLabel(String label)
Sets the label for this menu item to the specified label.
Parameters:
label - the new label, or null for no label.
Since:
JDK1.0
See Also:
getLabel()
!
isEnabled
public boolean isEnabled()
Checks whether this menu item is enabled.
Since:
JDK1.0
See Also:
setEnabled(boolean)
!
setEnabled
public void setEnabled(boolean b)
Sets whether or not this menu item can be chosen.
Parameters:
b - if true, enables this menu item; if false, disables it.
Since:
JDK1.1
See Also:
isEnabled()
!
enable
public void enable()
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
!
enable
public void enable(boolean b)
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
!
disable
public void disable()
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
!
getShortcut
public MenuShortcut getShortcut()
Get the MenuShortcut object associated with this menu item,
Returns:
the menu shortcut associated with this menu item, or null if none has been specified.
Since:
JDK1.1
See Also:
setShortcut(java.awt.MenuShortcut)
!
setShortcut
public void setShortcut(MenuShortcut s)
Set the MenuShortcut object associated with this menu item. If a menu shortcut is already associated with this menu item, it is replaced.
Parameters:
s - the menu shortcut to associate with this menu item.
Since:
JDK1.1
See Also:
getShortcut()
!
deleteShortcut
public void deleteShortcut()
Delete any MenuShortcut object associated with this menu item.
Since:
JDK1.1
!
enableEvents
protected final void enableEvents(long eventsToEnable)
Enables event delivery to this menu item for events to be defined by the specified event mask parameter
Since event types are automatically enabled when a listener for that type is added to the menu item, this method only needs to be invoked by subclasses of MenuItem which
desire to have the specified event types delivered to processEvent regardless of whether a listener is registered.

Parameters:
eventsToEnable - the event mask defining the event types
Since:
JDK1.1
See Also:
processEvent(java.awt.AWTEvent), disableEvents(long), Component.enableEvents(long)
!
disableEvents
protected final void disableEvents(long eventsToDisable)
Disables event delivery to this menu item for events defined by the specified event mask parameter.
Parameters:
eventsToDisable - the event mask defining the event types
Since:
JDK1.1
See Also:
processEvent(java.awt.AWTEvent), enableEvents(long), Component.disableEvents(long)
!
setActionCommand
public void setActionCommand(String command)
Sets the command name of the action event that is fired by this menu item.
By default, the action command is set to the label of the menu item.
Parameters:
command - the action command to be set for this menu item.
Since:
JDK1.1
See Also:
getActionCommand()
!
getActionCommand
public String getActionCommand()
Gets the command name of the action event that is fired by this menu item.
Since:
JDK1.1
See Also:
setActionCommand(java.lang.String)
!
addActionListener
public void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this menu item. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
removeActionListener(java.awt.event.ActionListener), getActionListeners(), ActionEvent, ActionListener
!
removeActionListener
public void removeActionListener(ActionListener l)
Removes the specified action listener so it no longer receives action events from this menu item. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
addActionListener(java.awt.event.ActionListener), getActionListeners(), ActionEvent, ActionListener
!
getActionListeners
public ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this menu item.
Returns:
all of this menu item's ActionListeners or an empty array if no action listeners are currently registered
Since:
1.4
See Also:
addActionListener(java.awt.event.ActionListener), removeActionListener(java.awt.event.ActionListener), ActionEvent,
ActionListener
!
getListeners
public EventListener[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this MenuItem. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a MenuItem m for its action listeners with the
following code:
ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));
If no such listeners exist, this method returns an empty array.
Parameters:
listenerType - the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns:
an array of all objects registered as FooListeners on this menu item, or an empty array if no such listeners have been added
Throws:
ClassCastException - if listenerType doesn't specify a class or interface that implements java.util.EventListener
Since:
1.3
See Also:
getActionListeners()
!
processEvent
protected void processEvent(AWTEvent e)
Processes events on this menu item. If the event is an instance of ActionEvent, it invokes processActionEvent, another method defined by MenuItem.

Currently, menu items only support action events.


Note that if the event parameter is null the behavior is unspecified and may result in an exception.
Overrides:
processEvent in class MenuComponent
Parameters:
e - the event
Since:
JDK1.1
See Also:
processActionEvent(java.awt.event.ActionEvent)
!
processActionEvent
protected void processActionEvent(ActionEvent e)
Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects. This method is not called unless action events are enabled
for this component. Action events are enabled when one of the following occurs:
An ActionListener object is registered via addActionListener.
Action events are enabled via enableEvents.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.
Parameters:
e - the action event
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, enableEvents(long)
!
paramString
public String paramString()
Returns a string representing the state of this MenuItem. This method is intended to be used only for debugging purposes, and the content and format of the returned string may
vary between implementations. The returned string may be empty but may not be null.
Overrides:
paramString in class MenuComponent
Returns:
the parameter string of this menu item
!
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this MenuItem. For menu items, the AccessibleContext takes the form of an AccessibleAWTMenuItem. A new
AccessibleAWTMenuItem instance is created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class MenuComponent
Returns:
an AccessibleAWTMenuItem that serves as the AccessibleContext of this MenuItem

java.awt
Class Panel
public class Panel
extends Container
implements Accessible
Panel is the simplest container class. A panel provides space in which an application can attach any other component, including other panels.

Constructor Summary

Panel()
Creates a new panel using the default layout manager.

Panel(LayoutManager layout)
Creates a new panel with the specified layout manager.

Method Summary

void addNotify()
Creates the Panel's peer.

AccessibleConte getAccessibleContext()
xt Gets the AccessibleContext associated with this Panel.

Constructor Detail

Panel
public Panel()
Creates a new panel using the default layout manager. The default layout manager for all panels is the FlowLayout class.
!
Panel
public Panel(LayoutManager layout)
Creates a new panel with the specified layout manager.
Parameters:
layout - the layout manager for this panel.
Since:
JDK1.1

Method Detail

addNotify
public void addNotify()
Creates the Panel's peer. The peer allows you to modify the appearance of the panel without changing its functionality.
Overrides:
addNotify in class Container
See Also:
Component.isDisplayable(), Container.removeNotify()
!
getAccessibleContext
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Panel. For panels, the AccessibleContext takes the form of an AccessibleAWTPanel. A new AccessibleAWTPanel instance is
created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class Component
Returns:
an AccessibleAWTPanel that serves as the AccessibleContext of this Panel

You might also like