ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Java Swing: Menus and Toolbars, Part 3
Pages: 1, 2, 3

The MenuDragMouseEvent Class

Swing generates a series of events while the mouse is dragging across an open menu. One event, MenuDragMouseEvent, describes the drag in relation to a particular menu item. You can listen for these events by adding an object that implements MenuDragMouseListener to the addMenuDragMouseListener( ) method of JMenuItem. The object implementing MenuDragMouseListener will have four separate methods that can be invoked in response to a mouse drag inside a menu; each one indicates exactly what happened with the drag. Table 14-5 shows the properties of the MenuDragMouseEvent.



Properties

Table 14-5: MenuDragMouseEvent properties

Property

Data type

get

is

set

Default value

clickCounto

int

·

 

 

 

ido

int

·

 

·

 

manager

MenuSelectionManager

·

 

 

 

modifierso

Object

·

 

·

 

path

MenuElement[ ]

·

 

 

 

popupTriggero

boolean

 

·

 

 

source

Object

·

 

 

 

wheno

long

·

 

 

 

xo

int

·

 

 

 

yo

int

·

 

·

 

ooverridden
See also java.awt.event.MouseEvent.

There are no defaults for the event; all properties are set in the constructor. The source property indicates the object that sent the event. The id property describes the type of event that was fired. The when property gives the event a timestamp. The modifiers property allows you to test various masks to see which mouse button is being pressed, as well as the Alt, Ctrl, Shift, and Meta keys. The x and y properties give the current location of the mouse pointer relative to the component in question. The clickCount property describes how many times a mouse button has been clicked prior to this drag. The popupTrigger property indicates whether this mouse event should cause a popup menu to appear. The path property gives an ordered array of MenuElement objects, describing the path to this specific menu. Finally, the manager property contains a reference to the current MenuSelectionManager for this menu system.

Constructor

public MenuDragMouseEvent(Component source, int id, long when, int modifiers, 
   int x, int y, int clickCount, boolean popupTrigger, MenuElement[ ] path, 
   MenuSelectionManager manager)

Initialize each of the properties described in Table 14-5 with the specified values.

The MenuDragMouseListener Interface

The MenuDragMouseListener interface, which is the conduit for receiving the MenuDragMouseEvent objects, contains four methods. One method is called when the mouse is dragged inside the menu item, the second when the mouse is released inside the menu item. Finally, the last two are called when the mouse is dragged into a menu item, or dragged out of a menu item.

Methods

public abstract void menuDragMouseDragged(PopupMenuEvent e)

Called when the mouse is dragged inside of a menu item.

public abstract void menuDragMouseReleased(PopupMenuEvent e)

Called when the mouse has been released inside of a menu item.

public abstract void menuDragMouseEntered(PopupMenuEvent e)

Called when the mouse is being dragged, and has entered a menu item.

public abstract void menuDragMouseExited(PopupMenuEvent e)

Called when the mouse is being dragged, and has exited a menu item.

The MenuKeyEvent Class

Swing also generates an event when a specific menu item receives a key event. Note that the key event does not have to be directed at the specific menu (i.e., an accelerator or mnemonic). Instead, the menu item responds to any key events generated while the menu pop up containing it is showing on the screen. You can listen for these events by adding an object that implements MenuKeyListener to the addMenuKeyListener( ) method of JMenuItem. The object implementing MenuKeyListener will have three separate methods that can be invoked in response to a menu key event.

Table 14-6 shows the properties of MenuKeyEvent. There are no defaults for the event; all properties are set in the constructor. The source property indicates the object that sent the event. The id property describes the type of event that was fired. The when property gives the event a timestamp. The modifiers property allows you to test various masks to see which mouse button is being pressed, as well as the Alt, Ctrl, Shift, and Meta keys. The keyCode and keyChar properties describe the key that was actually pressed. The path property gives an ordered array of MenuElement objects, describing the path to this specific menu. Finally, the manager property contains a reference to the current MenuSelectionManager.

Table 14-6: MenuKeyEvent properties

Property

Data Type

get

is

set

Default Value

ido

int

·

 

·

 

keyCharo

char

·

 

·

 

keyCodeo

int

·

 

 

 

manager

MenuSelectionManager

·

 

 

 

modifierso

Object

·

 

·

 

path

MenuElement[ ]

·

 

 

 

source

Object

·

 

 

 

wheno

long

·

 

 

 

ooverridden
See also java.awt.event.keyEvent.

Constructor

public MenuDragMouseEvent(Component source, int id, long when, 
   int keyCode, char keyChar, MenuElement[ ] path, MenuSelectionManager manager)

This constructor takes each of the properties described in Table 14-6.

The MenuKeyListener Interface

The MenuKeyListener interface, which is the conduit for receiving the MenuKeyEvent objects, contains three methods. One method is called when a key is typed (i.e., pressed and released) while the second is called after a key is pressed. This third is called after a key is released. Note that if a key is pressed and held down for a few seconds, Swing emulates the traditional key behavior: it considers the key both "typed" and "pressed" again.

Methods

public abstract void menuKeyTyped(MenuKeyEvent e)

Called when a key intended for this menu element is both pressed and released.

public abstract void menuKeyPressed(MenuKeyEvent e)

Called when a key intended for this menu element is pressed.

public abstract void menuKeyReleased(MenuKeyEvent e)

Called when a key intended for this menu element is released.

Menu items cannot exist by themselves; they must be embedded in menus. Swing implements two closely related styles of menus: anchored menus and pop-up menus. Swing uses the JMenu and JPopupMenu classes to implement these menus.

In the next installment, learn about the JPopupMenu Class.


Return to ONJava.com.