Class MenuItem in Java

0
24
Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

Class MenuItem in Java

MenuItem class encapsulates a menu item with a specified textual label. A MenuItem can be added to a menu pane with the Menu.add() method. The disable() method makes an item non-selectable; we can use it to “gray-out” a menu item when the command it represents is not valid in the current context.

The enable() method makes an item selectable again. we need to use setActionCommand() to specify an identifying string that is included in ActionEvent events generated by the menu item.

In short, the MenuItem class represents a selectable item on a menu.

The class structure for MenuItem is given as

public class java.awt.MenuItem extends java.awt.MenuComponent{
// Public Constructors
public MenuItem();
public MenuItem(String label);
public MenuItem(String label, MenuShortcut s);
// Public Instance Methods
public synchronized void addActionListener(ActionListener l);
public void addNotify();
public void deleteShortcut();
public synchronized void disable();
public synchronized void enable();
public void enable(boolean b);
public String getActionCommand();
public String getLabel();
public MenuShortcut getShortcut();
public boolean isEnabled();
public String paramString(); // Overrides MenuComponent
public synchronized void removeActionListener(ActionListener l);
public void setActionCommand(String command);
public synchronized void setEnabled(boolean b);
public synchronized void setLabel(String label);
public void setShortcut(MenuShortcut s);
// Protected Instance Methods
protected final void disableEvents(long eventsToDisable);
protected final void enableEvents(long eventsToEnable);
protected void processActionEvent(ActionEvent e);
protected void processEvent(AWTEvent e); // Overrides MenuComponent
}

The details of the class structure are given as follows:

public MenuItem();

public MenuItem() constructs a MenuItem object with no label or shortcut.

public MenuItem(String label);

public MenuItem(String label) constructs a MenuItem object with the label specified.

Parameter
label – Text that appears on the MenuItem.

public MenuItem(String label, MenuShortcut s);

public MenuItem(String label, MenuShortcut s) constructs a MenuItem object with the given shortcut.

Parameter
label – Text that appears on the MenuItem.
s – Shortcut for the MenuItem.

public void addActionListener (ActionListener l);

public void addActionListener (ActionListener l) method adds a listener for the action event.

Parameter
l – An object that implements the ActionListener interface.

public void addNotify();

public void addNotify() method creates the MenuItem’s peer.

public void deleteShortcut();

public void deleteShortcut() method removes the shortcut associated with this item.

public synchronized void disable();

public synchronized void disable() method disables the menu component so that it is unresponsive to user interactions. This method is now replaced by setEnabled(false).

public synchronized void enable();

public synchronized void enable() method enables the menu component so that it is responsive to user interactions. This method is now replaced by setEnabled(true).

public void enable(boolean condition);

public void enable(boolean condition) method enables or disables the menu component, depending on the condition parameter. This method is now replaced by setEnabled(boolean).

Parameter
condition – true to enable the menu component; false to disable it.

public String getActionCommand();

public String getActionCommand() method returns the string used for the action command.

This method returns the current action command string.

public String getLabel();

public String getLabel() method returns the current text associated with the MenuItem.

This method returns the current text associated with the MenuItem.

public MenuShortcut getShortcut();

public MenuShortcut getShortcut() method returns the current shortcut for this item, or null if there is none.

This method returns the current shortcut for this item, or null if there is none.

public boolean isEnabled();

public boolean isEnabled() method returns true if the menu item is enabled, false otherwise.

This method returns true if the menu item is enabled, false otherwise.

public String paramString();

public String paramString() method is a helper method for toString() to generate string of current settings.

This method returns a String with current settings of MenuItem.

public void removeActionListener (ActionListener l);

public void removeActionListener (ActionListener l) method removes an action event listener.

Parameter
l – One of this Button’s ActionListeners.

public void setActionCommand (String command);

public void setActionCommand (String command) method specifies the string used for the action command.

Parameter
command – New action command string.

public synchronized void setEnabled (boolean b);

public synchronized void setEnabled (boolean b) method enables or disables the item. Replaces enable(), enable(boolean), and disable().

Parameter
b – true to enable the item, false to disable it.

public synchronized void setLabel (String label);

public synchronized void setLabel (String label) method changes the label of the MenuItem.

Parameter
label – New text to appear on MenuItem.

public void setShortcut (MenuShortcut s);

public void setShortcut (MenuShortcut s) method changes the shortcut of the MenuItem.

Parameter
s – New shortcut for the MenuItem.

protected final void disableEvents (long eventsToDisable);

protected final void disableEvents (long eventsToDisable) method can be used to disable event type(By default, a menu item receives events corresponding to the event listeners that have registered. If a menu item should not receive events of a certain type, even if there is a listener registered for that type of event).

Parameter
eventsToDisable – A value representing certain kinds of events. This can be constructed by ORing the event mask constants defined in java.awt.AWTEvent.

protected final void enableEvents (long eventsToEnable);

protected final void enableEvents (long eventsToEnable) method can be used to get an event(By default, a menu item receives events corresponding to the event listeners that have registered. If a menu item should receive other types of events as well).
This method returns
Parameter
eventsToDisable – A value representing certain kinds of events. This can be constructed by ORing the event mask constants defined in java.awt.AWTEvent.

protected void processActionEvent (ActionEvent e);

protected void processActionEvent (ActionEvent e) method accepts an action events for processing. Normally, this method is called by processEvent().

Parameter
e – The action event to process.

protected void processEvent (AWTEvent e);

protected void processEvent (AWTEvent e) method accepts a low-level AWTEvents for processing.

Parameter
e – The action event to process.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here