[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add JPopupMenu in a JMenuBar

Posted on 2004-11-29
6
Medium Priority
?
248 Views
Last Modified: 2009-12-16
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
0
Comment
Question by:jorgearias
  • 2
  • 2
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12700699
Add it as a JMenu
0
 
LVL 1

Author Comment

by:jorgearias
ID: 12700749
Sorry "objects", I'm new in Java. I'm afraid i didn´t understand you
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12700772
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 12700776
Use a JMenu instead of a JPopupMenu
0
 
LVL 1

Author Comment

by:jorgearias
ID: 12700797
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
 
LVL 3

Accepted Solution

by:
udo_borkowski earned 500 total points
ID: 12701040
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question