Explain different classes used in designing menus with example.

 1) JMenuBar

Almost every top-level window has a menu bar associated with it. This menu bar consists of various menusavailable to the end user. The JMenuBar class is used to display menubar on the window or frame. It may have several menus.We can create menu bar by using following constructor:

JMenuBar()

JMenuBar class declaration

public class JMenuBar extends JComponent implements MenuElement, Accessible  


2) JMenu

The Menu class represents pull-down menu component which is deployed from a menu bar.The object of JMenu class is a pull down menu component which is displayed from the menu bar. It inherits the JMenuItem class. It can be created by using following constructors:

Menu()

Menu(String)

JMenu class declaration

public class JMenu extends JMenuItem implements MenuElement, Accessible  


3) JMenuItem

The JMenultem class represents the actual item in a menu. All items in a menu should derive from class JMenultem, or one of its subclasses. By default, it embodies a simple labeled menu item. The object of JMenuItem class adds a simple labeled menu item. The items used in a menu must belong to the JMenuItem or any of its subclass.It can be created by using following constructors:

MenuItem()

JMenultem(String text)

MenuItem(Icon icn)

JMenultem(String text, Icon icn)

JMenultem(String text, int mnemonic)

JMenuItem class declaration

public class JMenuItem extends AbstractButton implements Accessible, MenuElement  


Example of Java JMenuItem and JMenu 

import javax.swing.*;  

class MenuExample  

{  

          JMenu menu, submenu;  

          JMenuItem i1, i2, i3, i4, i5;  

          MenuExample(){  

          JFrame f= new JFrame("Menu and MenuItem Example");  

          JMenuBar mb=new JMenuBar();  

          menu=new JMenu("Menu");  

          submenu=new JMenu("Sub Menu");  

          i1=new JMenuItem("Item 1");  

          i2=new JMenuItem("Item 2");  

          i3=new JMenuItem("Item 3");  

          i4=new JMenuItem("Item 4");  

          i5=new JMenuItem("Item 5");  

          menu.add(i1); menu.add(i2); menu.add(i3);  

          submenu.add(i4); submenu.add(i5);  

          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 MenuExample();  

}}  

Output:


 4) JCheckBoxMenuItem

The JCheckboxMenuItem class represents a check box which can be included in a menu.

Selecting the check box in the menu changes control's state from on to off or from off to on. It can be created by using following constructors:

JCheckBoxMenuItem()

JCheckBoxMenuItem(String text) JCheckBoxMenuItem(Icon icn)

JCheckBoxMenultem(String text, boolean state)

JCheckBoxMenuItem(String text, Icon icn)

JCheckBoxMenuItem(String text, Icon icn, boolean state)


5) JRadioButton MeniItem

The JRadioButtonMenuItem class represents a Radio Button that can be included in a menu. Selecting the Radio Button in the menu changes the control's state from on to off or from off to on. It can be created by using the following constructors:

JRadioButtonMenuItem()

JRadioButtonMenultem(String text)

JRadioButtonMenuItem(Icon icn)

JRadion ButtonMenultem(String text, boolean state) 

JRadioButtonMenultem(Icon icn, boolean state)

JRadionButtonMenultem(String text, Icon icn) 

JRadionButton MenuItem(String text, Icon icn, boolean state)


6. Java JPopupMenu

PopupMenu can be dynamically popped up at specific position within a component. It inherits the JComponent class.


JPopupMenu class declaration

Let's see the declaration for javax.swing.JPopupMenu class.

public class JPopupMenu extends JComponent implements Accessible, MenuElement  


Commonly used Constructors:

JPopupMenu()= Constructs a JPopupMenu without an "invoker".

JPopupMenu(String label)= Constructs a JPopupMenu with the specified title.


Java JPopupMenu Example

import javax.swing.*;  

import java.awt.event.*;  

class PopupMenuExample  

{  

     PopupMenuExample(){  

         final JFrame f= new JFrame("PopupMenu Example");  

         final JPopupMenu popupmenu = new JPopupMenu("Edit");   

         JMenuItem cut = new JMenuItem("Cut");  

         JMenuItem copy = new JMenuItem("Copy");  

         JMenuItem paste = new JMenuItem("Paste");  

         popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);        

         f.addMouseListener(new MouseAdapter() {  

            public void mouseClicked(MouseEvent e) {              

                popupmenu.show(f , e.getX(), e.getY());  

            }                 

         });  

         f.add(popupmenu);   

         f.setSize(300,300);  

         f.setLayout(null);  

         f.setVisible(true);  

     }  

public static void main(String args[])  

{  

        new PopupMenuExample();  

}}  

Output:




7.Java JCheckBoxMenuItem

JCheckBoxMenuItem class represents a checkbox that can be included on a menu. A CheckBoxMenuItem can have text or a graphic icon or both, associated with it. MenuItem can be selected or deselected. MenuItems can be configured and controlled by actions.

Constructor

JCheckBoxMenuItem()//It creates an initially unselected check box menu item with no set text or icon.

JCheckBoxMenuItem(Action a)//It creates a menu item whose properties are taken from the Action supplied.

JCheckBoxMenuItem(Icon icon)//It creates an initially unselected check box menu item with an icon.

JCheckBoxMenuItem(String text)//It creates an initially unselected check box menu item with text.

JCheckBoxMenuItem(String text, boolean b)//It creates a check box menu item with the specified text and selection state.

JCheckBoxMenuItem(String text, Icon icon)//It creates an initially unselected check box menu item with the specified text and icon.

JCheckBoxMenuItem(String text, Icon icon, boolean b)//It creates a check box menu item with the specified text, icon, and selection state.


Java JCheckBoxMenuItem Example

import java.awt.event.ActionEvent;  

import java.awt.event.ActionListener;  

import java.awt.event.KeyEvent;  

import javax.swing.AbstractButton;  

import javax.swing.Icon;  

import javax.swing.JCheckBoxMenuItem;  

import javax.swing.JFrame;  

import javax.swing.JMenu;  

import javax.swing.JMenuBar;  

import javax.swing.JMenuItem;  

public class JavaCheckBoxMenuItem {  

    public static void main(final String args[]) {  

        JFrame frame = new JFrame("Jmenu Example");  

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        JMenuBar menuBar = new JMenuBar();  

        // File Menu, F - Mnemonic  

        JMenu fileMenu = new JMenu("File");  

        fileMenu.setMnemonic(KeyEvent.VK_F);  

        menuBar.add(fileMenu);  

        // File->New, N - Mnemonic  

        JMenuItem menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);  

        fileMenu.add(menuItem1);  

        JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Option_1");  

        caseMenuItem.setMnemonic(KeyEvent.VK_C);  

        fileMenu.add(caseMenuItem);  

        ActionListener aListener = new ActionListener() {  

            public void actionPerformed(ActionEvent event) {  

                AbstractButton aButton = (AbstractButton) event.getSource();  

                boolean selected = aButton.getModel().isSelected();  

                String newLabel;  

                Icon newIcon;  

                if (selected) {  

                    newLabel = "Value-1";  

                } else {  

                    newLabel = "Value-2";  

                }  

                aButton.setText(newLabel);  

            }  

        };  

        caseMenuItem.addActionListener(aListener);  

        frame.setJMenuBar(menuBar);  

        frame.setSize(350, 250);  

        frame.setVisible(true);  

    }  

}  

Output:


Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC