Results 1 to 1 of 1
  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default How to add a PopupMenu in java?

    In this tutorial, we will illustrate how to add a PopupMenu. A PopupMenu is similar to a Menu as it contains MenuItem objects. The Pop-up Menu can be popped over any component while generating the appropriate mouse event rather than letting it appear at the top of a Frame. In the example below, we have used MouseEvent.isPopupTrigger() method to trigger the MouseEvent that pops up the menu. The example below shows the triggering of a pop-up menu and its activation through a command button.

    Java Code: Program to add a PopupMenu
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    public class PopupMenuDemo extends Applet {
    
        Button b;
        TextField msg;
        PopupAppMenu m;
    
        public PopupMenuDemo() {
            setSize(200, 200);
            b = new Button("Pop-up Menu");
            add(b, BorderLayout.NORTH);
            msg = new TextField();
            msg.setEditable(false);
            add(msg, BorderLayout.SOUTH);
            m = new PopupAppMenu(this);
            add(m);
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    m.show(b, 20, 20);
                }
            });
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        m.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
    
                public void mouseReleased(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        m.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            });
        }
    
        public static void main(String[] args) {
            PopupMenuDemo app = new PopupMenuDemo();
            app.setVisible(true);
        }
    }
    
    class PopupAppMenu extends PopupMenu
            implements ActionListener {
    
        PopupMenuDemo ref;
    
        public PopupAppMenu(PopupMenuDemo ref) {
            super("File");
            this.ref = ref;
            MenuItem mi;
            add(mi = new MenuItem("Copy"));
            mi.addActionListener(this);
            add(mi = new MenuItem("Open"));
            mi.addActionListener(this);
            add(mi = new MenuItem("Cut"));
            mi.addActionListener(this);
            add(mi = new MenuItem("Paste"));
            mi.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent e) {
            String item = e.getActionCommand();
            ref.msg.setText("Option Selected: " + item);
        }
    }
    Output of the program when it is run:

    How to add a PopupMenu in java?-add-popupmenu.jpg
    Figure: Window showing PopupMenu
    Last edited by Java Example; 09-11-2014 at 04:32 PM.

Similar Threads

  1. Replies: 3
    Last Post: 05-12-2012, 03:57 PM
  2. Replies: 10
    Last Post: 05-08-2012, 05:17 AM
  3. scroll popupmenu
    By jperson in forum New To Java
    Replies: 1
    Last Post: 11-18-2010, 05:17 AM
  4. Hello!! I need to add a popupMenu to a menu
    By andr33i_yo in forum AWT / Swing
    Replies: 10
    Last Post: 12-30-2008, 12:22 PM
  5. various actions in menuitem of popupmenu
    By Peggy in forum Java Applets
    Replies: 0
    Last Post: 12-11-2008, 11:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •