Add JPopupMenu in a JMenuBar

Hi

I'm working with a popup menu but in some OS there are not something like right click. For this reason, I'm tring to show the popup menu that is showed, in the main menu bar of my application when some action is made.
How can I do this?

I'll thank any help

Jorge
LVL 1
jorgeariasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
Add it as a JMenu
0
jorgeariasAuthor Commented:
Sorry "objects", I'm new in Java. I'm afraid i didn´t understand you
0
udo_borkowskiCommented:
1. ...  but in some OS there are not something like right click ...

Just as a side node: instead of checking for a "right" click one should use the "isPopupTrigger()" method to check if a popup menu should "pop up" for better cross-platform support. The check should be both in the mousePressed and mouseReleased method (since on some platforms the popup may appear on mouseDown, on others on mouseUp):

aComponent.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent event) {
            if (event.isPopupTrigger()) {
                menu.show(event.getComponent(), event.getX(), event.getY());
            }
        }

        public void mouseReleased(MouseEvent event) {
             if (event.isPopupTrigger()) {
                menu.show(event.getComponent(), event.getX(), event.getY());
            }
        }
}


0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

objectsCommented:
Use a JMenu instead of a JPopupMenu
0
jorgeariasAuthor Commented:
udo_borkowski: I'v already try isPopupTrigger() but it didn´t work in my versión of Linux so I decided to check for a "right" click.
0
udo_borkowskiCommented:
See the following complete sample how to use the same JMenuItems both in a "classic" Menu and a popup menu:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;


public class MenuSampleMainFrame extends JFrame {

    MenuSampleMainFrame() {
        addClassicMenu();
        addPopupSupport();
    }

    private void addClassicMenu() {
        // add the menuitem to a JMenu, attached to the frames Menubar
        JMenu classicMenu = new JMenu("My Menu");
        JMenuItem[] menuItems = getMenuItems();
        for (int i = 0; i < menuItems.length; i++) {
            classicMenu.add(menuItems[i]);
        }
        JMenuBar menubar = new JMenuBar();
        menubar.add(classicMenu);

        this.setJMenuBar(menubar);
    }

    private JMenuItem[] addPopupSupport() {
        // create a popup Menu with sample items
        final JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem[] menuItems = getMenuItems();
        for (int i = 0; i < menuItems.length; i++) {
            popupMenu.add(menuItems[i]);
        }

        // A click into the frame should open the popup
        getContentPane().addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent event) {
                if (event.isPopupTrigger()) {
                    popupMenu.show(event.getComponent(), event.getX(), event
                            .getY());
                }
            }

            public void mouseReleased(MouseEvent event) {
                if (event.isPopupTrigger()) {
                    popupMenu.show(event.getComponent(), event.getX(), event
                            .getY());
                }
            }
        });
        return menuItems;
    }

    /**
     * @return an array with three sample menu items.
     */
    private JMenuItem[] getMenuItems() {
        // create the result array
        JMenuItem result[] = new JMenuItem[3];

        // add first item to result
        result[0] = new JMenuItem("Sample Item 1");
        result[0].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Item 1 selected");
            }
        });

        // add first item to result
        result[1] = new JMenuItem("Sample Item 2");
        result[1].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Item 2 selected");
            }
        });
        // add first item to result
        result[2] = new JMenuItem("Sample Item 3");
        result[2].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Item 3 selected");
            }
        });

        return result;
    }

    public static void main(String[] args) {
        MenuSampleMainFrame sample = new MenuSampleMainFrame();

        sample.setSize(300, 200);
        sample.show();
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.