IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Menu Bar, Menus, Menu Items and Listeners

2 posters

Go down

Menu Bar, Menus, Menu Items and Listeners Empty Menu Bar, Menus, Menu Items and Listeners

Post by BIT0112-Rokon Tue Oct 27, 2009 6:31 am

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:



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:



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:



Menu Bar, Menus, Menu Items and Listeners Jmenub11



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.



Source: more about it Click here.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Menu Bar, Menus, Menu Items and Listeners Empty Re: Menu Bar, Menus, Menu Items and Listeners

Post by BIT0104-ANIK Thu Oct 29, 2009 10:02 am

u r really good..
BIT0104-ANIK
BIT0104-ANIK
Administrator
Administrator

Course(s) :
  • BIT

Blood Group : O+
Posts : 423
Points : 699

http://anikstech.blogspot.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum