Java Swing: Menus and Toolbars, Part 3

Java Swing: Menus and Toolbars, Part 3

Properties

The properties for the JMenuItem class are shown in Table 14-4. Most of the properties shown are superclass properties reconfigured to ensure that the menu item's "button" acts like a menu item should. The borderPainted property is always false; menu items never take a border. The focusPainted property is also false to ensure that a focus rectangle is never drawn around the menu item. horizontalTextPosition and horizontalAlignment are both initialized to JButton.LEFT. This places the text to the left of the image icon and places the text and image icon on the left side of the menu item. (See the previous example for information on how to reconfigure this.)

Table 14-4: JMenuItem properties

Property

Data type

get

is

set

Default value

acceleratorb

KeyStroke

·

 

·

null

accessibleContexto

Accessible Context

·

 

 

JMenuItem.AccessibleJMenuItem( )

armedb, o

boolean

 

· ·

false

borderPaintedo

boolean

 

· ·

false

componento

Component

·

 

 

 

enabledo

boolean

 

· ·

true

focusPaintedo

boolean

 

· ·

false

horizontalAlignmento

int

·

 

·

JButton.LEFT

horizontalTextPositiono

int

·

 

·

JButton.LEFT

menuDragMouseListeners1.4

MenuDragMouseListener[ ]

·

 

 

 

menuKeyListeners1.4

MenuKeyListener[ ]

·

 

 

 

modelo

ButtonModel

·

 

·

DefaultButtonModel( )

subElementso

MenuElement[ ]

·

 

 

 

UIb

MenuItemUI

 

 

· From L&F

UIClassIDo

String

·

 

 

"MenuItemUI"

1.4since 1.4, bbound, ooverridden
See also properties from the AbstractButton class (Table 5-4).

The accelerator property sets the keyboard accelerator for the menu item; the accelerator is typically drawn to the right of the menu item string. The armed property simply maps a boolean down to the armed state of the component model, ButtonModel. You can use this to programmatically select the menu item, if needed. The enabled property is a boolean that indicates whether the user can select the menu item. If the menu item is disabled, JMenuItem automatically grays the text and associated image. As discussed earlier, the most powerful way to control the enabled state of a menu item is to associate it with an Action object so that it automatically tracks the action's enabled state. The subElements property provides an array of submenus contained in this menu item.

Constructors

JMenuItem(  )

JMenuItem(Action action)

JMenuItem(Icon icon)

JMenuItem(String string)

JMenuItem(String string, Icon icon)

JMenuItem(String string, int mnemonic)

Create a menu item with the appropriate icon or string. You also have the option to specify a mnemonic if you initialize with a string. Since Version 1.3, you can use the properties of an Action to directly configure the properties of the JMenuItem.

Events

JMenuItems send many different kinds of events. Perhaps the most important are ActionEvents, which are fired when an item is selected. ChangeEvents are fired when button properties change. Methods for adding and removing listeners for these events are inherited from AbstractButton.

JMenuItem also uses special events for reporting mouse motions and key presses on top of the menu item. These are the MenuDragMouseEvent and MenuKeyEvent. Here are the methods for registering listeners for these events:

addMenuDragMouseListener (MenuDragMouseListener 1)

removeMenuDragMouseListener (MenuDragMouseListener 1)

These methods add or remove a specific MenuDragMouseListener interested in being notified when there is a MenuDragMouseEvent.

addMenuKeyListener (MenuKeyListener 1)

removeMenuKeyListener (MenuKeyListener 1)

These methods add or remove a specific MenuKeyListener interested in being notified when there is a MenuKeyEvent.

The following methods provide support for firing these events, though you will probably never need to call them:

public void processMenuDragMouseEvent (MenuDragMouseEvent e)

Fire a specific MenuDragMouseEvent notification based on the type of MouseEvent that was observed. If the MouseEvent listed was MOUSE_ENTERED, for example, the menu invokes the fireMenuDragMouseEntered( ) method.

public void processMenuKeyEvent (MenuKeyEvent e)

Fire a specific MenuKeyEvent notification based on the type of MenuKeyEvent that was observed. If the MenuKeyEvent listed was KEY_RELEASED, for example, the menu invokes the fireMenuKeyReleased( ) method.

Method

public void updateUI(  )

Force the current UI manager to reset the current delegate for the component, thus updating the component's L&F.

Menu Element Interface

public void menuSelectionChanged(boolean isIncluded)

public MenuElement[ ] getSubElements(  )

public Component getComponent(  )

public void processMouseEvent(MouseEvent event, 

   MenuElement path[ ], MenuSelectionManager manager)

public void processKeyEvent(KeyEvent event, MenuElement path[ ], 

   MenuSelectionManager manager)

Implement the MenuElement interface, discussed later in this chapter.

Prev  [1] [2] [3] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java Basic > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction