Menu Bar, Menus, Menu Items and Listeners
2 posters
Page 1 of 1
Menu Bar, Menus, Menu Items and Listeners
Menu Bar, Menus, Menu Items and Listeners
javax.swing.JMenuBar - A Swing class representing the menu bar on the main frame. javax.swing.JMenu objects added a JMenuBar object will be displayed horizontally. Interesting methods of JMenuBar include:
JMenuBar() - Constructor to create a menu bar which can be added to a frame window.
add(JMenu) - Method to add a new JMenu object to the end of the menu be list.
getMenuCount() - Method to return the number of menus in this menu bar.
getMenu(int) - Method to return the JMenu object of the specified position index. Of course, index 0 is the first menu.
getSubElements() - Method to return an array of MenuElement objects.
isSelected() - Method to return true if an element is currently selected in the menu bar.
javax.swing.JMenu - A Swing class representing a user interface menu. A JMenu object can be added to a JMenuBar or another JMenu object to form a menu tree structure. A JMenu object actually has two graphical components, a clickable button displayed in the parent JMenu or JMenuBar and a popup window. When its button is clicked, its pop up window will be displayed. Interesting methods of JMenu include:
javax.swing.JMenu - A Swing class representing a user interface menu. A JMenu object can be added to a JMenuBar or another JMenu object to form a menu tree structure. A JMenu object actually has two graphical components, a clickable button displayed in the parent JMenu or JMenuBar and a popup window. When its button is clicked, its pop up window will be displayed. Interesting methods of JMenu include:
JMenu(String) - Constructor to create a menu with the specified string as its button text.
add(JMenuItem) - Method to add a JMenuItem object to the end of its current menu item list.
addSeparator() - Method to add a menu separator to the end of its current menu item list.
getItemCount() - Method to return the number of its menu items.
getItem(int pos) - Method to return the JMenuItem object of the specified position index. Of course, index 0 is the first menu item.
isSelected() - Method to return true if this menu is selected.
isTopLevelMenu() - Method to return true if this menu is a top menu. A top menu is a menu added to the menu bar, not to another menu.
doClick(int) - Method to simulate a user click on this menu.
javax.swing.JMenuItem - A Swing class representing a user interface menu item. A JMenuItem object can be added to a JMenuBar or another JMenu object in a menu tree structure. A JMenuItem should be associated with a MenuKeyListener object so that tasks can be performed with the menu item is clicked. Interesting methods of JMenuItem include:
javax.swing.JMenuItem - A Swing class representing a user interface menu item. A JMenuItem object can be added to a JMenuBar or another JMenu object in a menu tree structure. A JMenuItem should be associated with a MenuKeyListener object so that tasks can be performed with the menu item is clicked. Interesting methods of JMenuItem include:
JMenuItem(String) - Constructor to create a menu item with the specified string as its button text.
addMenuKeyListener(MenuKeyListener) - Method to add a MenuKeyListener object to this menu item.
setEnabled(boolean) - Method to set this menu item to be enabled or disabled based on the specified Boolean value.
Here is an example program I wrote to test the JMenuBar class:
- Code:
import java.awt.event.*;
import javax.swing.*;
public class JMenuBarTest {
public static void main(String[] a) {
JFrame f = new JFrame("JMenuBar Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(50,50,250,150);
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("Tools"));
mb.add(new JMenu("Options"));
mb.add(new JMenuItem("Save"));
mb.add(new JMenuItem("Quit"));
mb.add(new JButton("Stop"));
f.setJMenuBar(mb);
f.getContentPane().add(new MyButton());
f.setVisible(true);
}
private static class MyButton extends JButton implements ActionListener {
public MyButton() {
super("Check");
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Check button clicked");
JFrame myFrame = (JFrame)
(this.getParent().getParent()).getParent().getParent();
JMenuBar myMenuBar = myFrame.getJMenuBar();
System.out.println("# of elements in the menu bar: "+myMenuBar.getMenuCount());
System.out.println("Is the menu bar selected: "+myMenuBar.isSelected());
}
}
}
If you run this example, you will see the frame window shows up with the menu bar like this:
If you click the "Check" button, click the "Options" menu in the menu bar, and click the "Check" button again, you will see text output in the console window:
C:\herong\swing_20051029\cod>java JMenuBarTest
Check button clicked
# of elements in the menu bar: 5
Is the menu bar selected: false
Check button clicked
# of elements in the menu bar: 5
Is the menu bar selected: true
Interesting notes about this example:
JMenuItem objects can be added to the menu bar in the same as JMenu objects.
Other components can also be added to the menu bar. See the "Stop" JButton object added in the example program.
The "Check" JButton added in the content pane is a grand grand grand child of the frame. So there are 3 layers of component in the content pane, because I have to use "this.getParent().getParent()).getParent().getParent()" to reach the frame object from the button object.
getMenuCount() and isSelected() methods work as expected.
getMenuCount() and isSelected() methods work as expected.
Source: more about it Click here.
Similar topics
» Edit your Context Menus: Hace- MMM
» Resize your image: from right click menu!!!
» Linux Mint Menu On Ubuntu
» Making the gnome menu a stylish one
» Help: Removing Windows from grub bootloader menu
» Resize your image: from right click menu!!!
» Linux Mint Menu On Ubuntu
» Making the gnome menu a stylish one
» Help: Removing Windows from grub bootloader menu
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford