Tuesday, 21 February 2017

Java Popup Menu

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class PopupTest extends JFrame
     {
     private JRadioButtonMenuItem items[];
     private Color colorValues[] = { Color.blue, Color.yellow, Color.red };
  
     public PopupTest()
         {
         super( "Using JPopupMenus" );
      
         final JPopupMenu popupMenu = new JPopupMenu();
         ItemHandler handler = new ItemHandler();
         String colors[] = { "Blue", "Yellow", "Red" };
         ButtonGroup colorGroup = new ButtonGroup();
         items = new JRadioButtonMenuItem[ 3 ];
      
         // construct each menu item and add to popup menu; also
         // enable event handling for each menu item
         for ( int i = 0; i < items.length; i++ )
             {
             items[ i ] = new JRadioButtonMenuItem( colors[ i ] );
             popupMenu.add( items[ i ] );
             colorGroup.add( items[ i ] );
             items[ i ].addActionListener( handler );
         }
      
         getContentPane().setBackground( Color.white );
      
         // define a MouseListener for the window that displays
         // a JPopupMenu when the popup trigger event occurs
         addMouseListener( new MouseAdapter()
             {
             public void mousePressed( MouseEvent e )
                 {
                 checkForTriggerEvent( e );
             }
          
             public void mouseReleased( MouseEvent e )
                 {
                 checkForTriggerEvent( e );
             }
          
             private void checkForTriggerEvent( MouseEvent e )
                 {
                 if ( e.isPopupTrigger() )
                 popupMenu.show( e.getComponent(), e.getX(), e.getY() );
             }
         } );
      
         setSize( 300, 200 );
         show();
     }
  
     public static void main( String args[] )
         {
         PopupTest app = new PopupTest();
      
         app.addWindowListener( new WindowAdapter()
             {
             public void windowClosing( WindowEvent e )
                 {
                 System.exit( 0 );
             }
         });
     }
  
     private class ItemHandler implements ActionListener
         {
         public void actionPerformed( ActionEvent e )
             {
             // determine which menu item was selected
             for ( int i = 0; i < items.length; i++ )
             if ( e.getSource() == items[ i ] )
                 {
                 getContentPane().setBackground( colorValues[ i ] );
                 repaint();
                 return;
             }
         }
     }
}

Java Menu

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/// MenuDemo
public class MenuDemo { 
   
    public static void main(String[] args) {
        JFrame win = new MenuDemoGUI();
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
    }
}

class MenuDemoGUI extends JFrame {
    JTextArea m_editArea = new JTextArea(20, 50);;
    JMenu     m_fileMenu = new JMenu("File");// declare and create new menu
    JMenuItem m_openItem = new JMenuItem("Open"); // create new menu item
    JMenuItem m_quitItem = new JMenuItem("Quit"); // another menu item
    JMenu     m_editMenu = new JMenu("Edit");
    JMenuItem m_copyItem = new JMenuItem("Copy");
    JMenuItem m_pasteItem= new JMenuItem("Paste");

    public MenuDemoGUI() {
        //... Add listeners to menu items
        m_openItem.addActionListener(new OpenAction());
        m_quitItem.addActionListener(new QuitAction());
        // Copy and Paste don't have listeners yet, so disable them for now.
        m_copyItem.setEnabled(false);
        m_pasteItem.setEnabled(false);
       
        //... Menubar, menus, menu items.  Use indentation to show structure.
        JMenuBar menubar = new JMenuBar();  // declare and create new menu bar
            menubar.add(m_fileMenu);        // add the menu to the menubar
                m_fileMenu.add(m_openItem); // add the menu item to the menu
                m_fileMenu.addSeparator();  // add separator line to menu
                m_fileMenu.add(m_quitItem);
            menubar.add(m_editMenu);
                m_editMenu.add(m_copyItem);
                m_editMenu.add(m_pasteItem);
       
        //... Content pane: create, layout, add components
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(m_editArea, BorderLayout.CENTER);

        //... Set JFrame's menubar, content pane, and title.
        this.setContentPane(content);       // Set windows content pane..
        this.setJMenuBar(menubar);          // Set windows menubar.
        this.pack();
        this.setTitle("MenuDemo");
    }//endconstructor
   
//// OpenAction
    class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Sorry, can't open anything");
        }
    }
   
/// QuitAction
    class QuitAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);  // terminate this program
        }
    }
}