Bitcoin

Bitcoin
Bitcoin

JMenuBar, JMenu and JMenuItem

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.

JMenuBar class declaration

public class JMenuBar extends JComponent implements Accessible, MenuElement

JMenu class declaration

public class JMenu extends JMenuItem implements Accessible, MenuElement

JMenuItem class declaration

public class JMenuItem extends AbstractButton implements Accessible, MenuElement 



Java JMenuItem and JMenu Examples

Creating a simple JMenuBar, JMenu, and JMenuItem Component
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create the menu bar
    JMenuBar menuBar = new JMenuBar();

    // Create a menu
    JMenu menu = new JMenu("Menu Label");
    menuBar.add(menu);

    // Create a menu item
    JMenuItem item = new JMenuItem("Item Label");
    //item.addActionListener(actionListener);
    menu.add(item);

    JFrame frame = new JFrame();
    // Install the menu bar in the frame
    frame.setJMenuBar(menuBar);
  }
}
A JMENUBAR, JMENU AND JMENUITEM EXAMPLES
import javax.swing.*;  
class JMenuExample  
{  
          JMenu menu, submenu;  
          JMenuItem item1, item2, item3, item4, item5;  
          JMenuExample(){  
               JFrame f= new JFrame("Menu and MenuItem Example");  
               JMenuBar mb=new JMenuBar();  
               menu=new JMenu("Menu");  
               submenu=new JMenu("Sub Menu");  
               item1=new JMenuItem("Item 1");  
               item2=new JMenuItem("Item 2");  
               item3=new JMenuItem("Item 3");  
               item4=new JMenuItem("Item 4");  
               item5=new JMenuItem("Item 5");  
               menu.add(item1); menu.add(item2); menu.add(item3);  
               submenu.add(item4); submenu.add(item5);  
               menu.add(submenu);  
               mb.add(menu);  
               f.setJMenuBar(mb);  
               f.setSize(400,400);  
               f.setLayout(null);  
               f.setVisible(true);  
    }  
     public static void main(String args[])  
    {  
          new JMenuExample();  
    }
}  
OUTPUT:

Example of creating Edit menu for Notepad:

import javax.swing.*;    
import java.awt.event.*;    
public class JMenuExample1 implements ActionListener{    
     JFrame f;    
     JMenuBar mb;    
     JMenu file,edit,help;    
     JMenuItem cut,copy,paste,selectAll;    
     JTextArea ta;    
     JMenuExample1(){    
           f=new JFrame();    
           cut=new JMenuItem("cut");    
           copy=new JMenuItem("copy");    
           paste=new JMenuItem("paste");    
           selectAll=new JMenuItem("selectAll");    
           cut.addActionListener(this);    
           copy.addActionListener(this);    
           paste.addActionListener(this);    
           selectAll.addActionListener(this);    
           mb=new JMenuBar();    
           file=new JMenu("File");    
           edit=new JMenu("Edit");    
           help=new JMenu("Help");     
           edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);    
           mb.add(file);mb.add(edit);mb.add(help);    
           ta=new JTextArea();    
           ta.setBounds(5,5,360,320);    
           f.add(mb);f.add(ta);    
           f.setJMenuBar(mb);  
           f.setLayout(null);    
           f.setSize(400,400);    
           f.setVisible(true);    
    }     
    public void actionPerformed(ActionEvent e) {    
        if(e.getSource()==cut)    
             ta.cut();    
       if(e.getSource()==paste)    
             ta.paste();    
      if(e.getSource()==copy)    
            ta.copy();    
      if(e.getSource()==selectAll)    
            ta.selectAll();    
   }     
   public static void main(String[] args) {    
           new JMenuExample1;    
   }    
}    
OUTPUT:






No comments:

Post a Comment

Facebook