Jun 23, 2015 Enable and Disable Menustrip in C#.net on Client form Click.
A menu provides a space-saving way to let the user choose one of several options. Other components with which the user can make a one-of-many choice include,. If any of your menu items performs an action that is duplicated by another menu item or by a tool-bar button, then in addition to this section you should read.Menus are unique in that, by convention, they aren't placed with the other components in the UI. Instead, a menu usually appears either in a menu bar or as a popup menu. A menu bar contains one or more menus and has a customary, platform-dependent location usually along the top of a window.
A popup menu is a menu that is invisible until the user makes a platform-specific mouse action, such as pressing the right mouse button, over a popup-enabled component. The popup menu then appears under the cursor.The following figure shows many menu-related components: a menu bar, menus, menu items, radio button menu items, check box menu items, and separators. As you can see, a menu item can have either an image or text, or both.
You can also specify other properties, such as font and color.The rest of this section teaches you about the menu components and tells you how to use various menu features:.Here is a picture of the inheritance hierarchy for the menu-related classes:As the figure shows, menu items (including menus) are simply. You might be wondering how a menu, if it's only a button, shows its menu items. The answer is that when a menu is activated, it automatically brings up a popup menu that displays the menu items.The following code creates the menus shown near the beginning of this menu section. The bold lines of code create and connect the menu objects; the other code sets up or customizes the menu objects.
You can find the entire program in. Other required files are listed in the. Note: Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second. For a way of implementing multiple components that do the same thing, see.Other methods in the preceding code include setAccelerator and setMnemonic, which are discussed a little later in.
The setAccessibleDescription method is discussed in.To detect when the user chooses a JMenuItem, you can listen for action events (just as you would for a ). To detect when the user chooses a JRadioButtonMenuItem, you can listen for either action events or item events, as described in. For JCheckBoxMenuItems, you generally listen for item events, as described in. //Setting the mnemonic when constructing a menu item:menuItem = new JMenuItem('A text-only menu item',KeyEvent.VKT);//Setting the mnemonic after creation time:menuItem.setMnemonic(KeyEvent.VKT);//Setting the accelerator:menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VKT, ActionEvent.ALTMASK));As you can see, you set a mnemonic by specifying theconstant corresponding to the key the user should press. To specify an accelerator you must use aobject, which combines a key (specified by a KeyEvent constant) and a modifier-key mask (specified by anconstant).
Note: Because popup menus, unlike regular menus, aren't always contained by a component, accelerators in popup menu items don't work unless the popup menu is visible.To bring up a popup menu , you must register a mouse listener on each component that the popup menu should be associated with. The mouse listener must detect user requests that the popup menu be brought up.The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled. In the Java look and feel, the customary trigger is either pressing the right mouse button (for a popup that goes away when the button is released) or clicking it (for a popup that stays up). Try this:.
Click the Launch button to run the MenuLayout Demo using. Alternatively, to compile and run the example yourself, consult the.Here's a picture of the menu layout that MenuLayoutDemo creates:The following tables list the commonly used menu constructors and methods.
The API for using menus falls into these categories:.Constructor or MethodPurposeCreates a menu bar.Adds the menu to the end of the menu bar.(in JApplet, JDialog, JFrame, JInternalFrame, JRootPane)Sets or gets the menu bar of an, or.Constructor or MethodPurposeCreates a menu. The string specifies the text to display for the menu. The Action specifies the text and other properties of the menu (see).Adds a menu item to the current end of the menu. If the argument is a string, then the menu automatically creates a JMenuItem object that displays the specified text.Adds a separator to the current end of the menu.Inserts a menu item or separator into the menu at the specified position. The first menu item is at position 0, the second at position 1, and so on. The JMenuItem and String arguments are treated the same as in the corresponding add methods.Removes the specified item(s) from the menu. If the argument is an integer, then it specifies the position of the menu item to be removed.Constructor or MethodPurposeCreates a popup menu.
The optional string argument specifies the title that a look and feel might display as part of the popup window.Adds a menu item to the current end of the popup menu. If the argument is a string, then the menu automatically creates a JMenuItem object that displays the specified text.Adds a separator to the current end of the popup menu.Inserts a menu item into the menu at the specified position. The first menu item is at position 0, the second at position 1, and so on. The Component argument specifies the menu item to add.Removes the specified item(s) from the menu. If the argument is an integer, then it specifies the position of the menu item to be removed.By default, Swing implements a menu's window using a lightweight component. This can cause problems if you use any heavyweight components in your Swing program, as described in.
(This is one of several reasons to avoid using heavyweight components.) As a workaround, invoke JPopupMenu.setLightWeightPopupEnabled(false).Display the popup menu at the specified x,y position (specified in that order by the integer arguments) in the coordinate system of the specified component.Constructor or MethodPurposeCreates an ordinary menu item. The icon argument, if present, specifies the icon that the menu item should display.
Similarly, the string argument specifies the text that the menu item should display. The integer argument specifies the keyboard mnemonic to use. You can specify any of the relevant VK constants defined in theclass.
For example, to specify the A key, use KeyEvent.VKA.The constructor with the Action parameter sets the menu item's Action, causing the menu item's properties to be initialized from the Action. Seefor details.Creates a menu item that looks and acts like a check box. The string argument, if any, specifies the text that the menu item should display. If you specify true for the boolean argument, then the menu item is initially selected (checked). Otherwise, the menu item is initially unselected.Creates a menu item that looks and acts like a radio button.
The string argument, if any, specifies the text that the menu item should display. If you specify true for the boolean argument, then the menu item is initially selected. Otherwise, the menu item is initially unselected.(in JCheckBoxMenuItem)Set or get the selection state of a check box menu item.If the argument is true, enable the menu item. Otherwise, disable the menu item.Set the mnemonic that enables keyboard navigation to the menu or menu item. Use one of the VK constants defined in the KeyEvent class.Set the accelerator that activates the menu item.Set the name of the action performed by the menu item.Add an event listener to the menu item. See for details.Set the Action associated with the menu item. Seefor details.Many of the preceding methods are inherited from AbstractButton.
See for information about other useful methods that AbstractButton provides.Menus are used in a few of our examples. ExampleWhere DescribedNotesThis section A simple example that creates all kinds of menus except popup menus, but doesn't handle events from the menu items.This section Adds event handling to MenuLookDemo.This section Adds popup menus to MenuDemo.This section Demonstrates affecting menu layout by adding an invisible components to the menu bar.This section Implements sideways-opening menus arranged in a vertical menu bar.Adds highlight detection to MenuDemo. To see this feature, click a menu and then move the mouse over any menu item or submenu. Once per second, the text area will be updated with information about the currently highlighted menu item, not to be confused with the menu item that the user eventually chooses. This demo uses the default, which tracks the state of the menu hierarchy.Uses Action objects to implement menu items that duplicate functionality provided by tool bar buttons.Brings up multiple identical frames, each with a menu in its menu bar.Uses a menu item to create windows.See thetutorial to learn how to create menus in JavaFX.