Results 1 to 11 of 11
  1. #1
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Hello!! I need to add a popupMenu to a menu

    Hello!I have a big problem...I need to add a popupMenu to a menu from menuBar...

    I want that when i press left click on menu to display the submenus and menuItems from that menu....but when i press right click on menu I want to display my popupMenu .

    Please help me!
    PS:my email address is andr33i_yo@yahoo.com

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default popup menu

    Yes i worked out with a popup menu....but a didn't succeed to do what i said before.I realy need to do that.Can you help me...with an example?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Popup Menu's problem

    I don't want to add a pupup menu to a menuBar...I want to add it to a menu from the menuBar.
    Ok...here it is an example:

    package components;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JPopupMenu;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    import javax.swing.JMenuBar;
    import javax.swing.KeyStroke;
    import javax.swing.ImageIcon;

    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;

    /* PopupMenuDemo.java requires images/middle.gif. */

    /*
    * Like MenuDemo, but with popup menus added.
    */
    public class PopupMenuDemo implements ActionListener, ItemListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";

    public JMenuBar createMenuBar() {
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescripti on(
    "The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item",
    KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescr iption(
    "This doesn't really do anything");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/middle.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_2, ActionEvent.ALT_MASK));
    menuItem.addActionListener(this);
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    menuItem.addActionListener(this);
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescripti on(
    "This menu does nothing");
    menuBar.add(menu);

    return menuBar;
    }

    public Container createContentPane() {
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    //Create a scrolled text area.
    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    //Add the text area to the content pane.
    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;
    }

    public void createPopupMenu() {
    JMenuItem menuItem;

    //Create the popup menu.
    JPopupMenu popup = new JPopupMenu();
    menuItem = new JMenuItem("A popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);
    menuItem = new JMenuItem("Another popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);

    //Add listener to the text area so the popup menu can come up.
    MouseListener popupListener = new PopupListener(popup);
    output.addMouseListener(popupListener);
    }

    public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem)(e.getSource());
    String s = "Action event detected."
    + newline
    + " Event source: " + source.getText()
    + " (an instance of " + getClassName(source) + ")";
    output.append(s + newline);
    output.setCaretPosition(output.getDocument().getLe ngth());
    }

    public void itemStateChanged(ItemEvent e) {
    JMenuItem source = (JMenuItem)(e.getSource());
    String s = "Item event detected."
    + newline
    + " Event source: " + source.getText()
    + " (an instance of " + getClassName(source) + ")"
    + newline
    + " New state: "
    + ((e.getStateChange() == ItemEvent.SELECTED) ?
    "selected":"unselected");
    output.append(s + newline);
    output.setCaretPosition(output.getDocument().getLe ngth());
    }

    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
    String classString = o.getClass().getName();
    int dotIndex = classString.lastIndexOf(".");
    return classString.substring(dotIndex+1);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = PopupMenuDemo.class.getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL);
    } else {
    System.err.println("Couldn't find file: " + path);
    return null;
    }
    }

    /**
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("PopupMenuDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    //Create/set menu bar and content pane.
    PopupMenuDemo demo = new PopupMenuDemo();
    frame.setJMenuBar(demo.createMenuBar());
    frame.setContentPane(demo.createContentPane());

    //Create and set up the popup menu.
    demo.createPopupMenu();

    //Display the window.
    frame.setSize(450, 260);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    class PopupListener extends MouseAdapter {
    JPopupMenu popup;

    PopupListener(JPopupMenu popupMenu) {
    popup = popupMenu;
    }

    public void mousePressed(MouseEvent e) {
    maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
    maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
    if (e.isPopupTrigger()) {
    popup.show(e.getComponent(),
    e.getX(), e.getY());
    }
    }
    }
    }

    k...it is an example from java.sun.com.Now,I want to add the popup menu from this example to the first menu:"A Menu" from the menuBar, than I want to add this popup menu to the submenu: "A Submenu" from the menu "A Menu".Can you help me?

    Thanks very much,
    Andrew.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    andr33i_yo, I'm really confusing what you want to do here.

    As you said, A Menu has a pop-up menu in this example. There is one item called A SubMenu, right? You want to have another pop-up menu on that A SubMenu item, is that right? So it's there. Correct me if I'm wrong.

  7. #7
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    K...I will tell you what I want to do.I want to create a kind of framework to create a menuBar in a frame dynamically.

    I want to be able to right-click on any of menus in the menuBar and bring up a JPopupMenu showing some actions that can be performed on the menu(add an item,add a checkBox,add a submenu..etc ). A left click would simply open the menu as usual (Firefox and Explorer has this functionality already).

    For example in FireFox or Internet Explorer if you left-click on menu "File" it will be display a popup menu but if you right-click on the same menu "File" it will be display a different popup menu.This is what I want to be able to do in my frame.

    Andrew.
    PS:I hope this time you will understand me...

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes, now it's clear what you want to do.

    Did you go through that example code found from the Suns' tutorial? I don't think so. ;)

    Did you see this code segment on the method createPopupMenu

    Java Code:
            //Add listener to the text area so the popup menu can come up.
            MouseListener popupListener = new PopupListener(popup);
            output.addMouseListener(popupListener);
    What have done here? NouseListner is added to the pop-up menu on the JTextAre, named as output. What happen when you right click on the JTextAre? You can see the pop-up menu by right-click. You want to do the same thing on your menu bar? So what you should do?

  9. #9
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Back

    I'm very sorry if you think that i didn't study this example.If you try to run this simple example and try to put this popup menu on menu "A Menu" for example you would know now that it is a problem.By default in java when you right-click on a menu form the menuBar it is hapeninig the same think as it is hapeninig when you left-click.Please try to do that in this example than write me if you succsed.


    Andrew.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I don't usually give out what I believe to be a full solution, but it is clear that you've already spent a lot of time on this.

    Also, I'm not sure this is the best way this can be done, but it does appear to function as desired.

    I didn't expect it, but when a JPopupMenu is parented to a JMenu, it remains inactive. Hence the parenting to the JMenuBar.

    Java Code:
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;
    
    public class MenuWithPopup {
       
       private Map<JMenu, MouseListener> menuMap = new HashMap<JMenu, MouseListener>();
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
    
             @Override
             public void run() {
                new MenuWithPopup().makeUI();
             }
          });
       }
    
       public void makeUI() {
          JMenuItem onePopupItem = new JMenuItem("Popup One");
          JMenuItem twoPopupItem = new JMenuItem("Popup Two");
          JMenuItem threePopupItem = new JMenuItem("Popup Three");
    
          final JPopupMenu popupMenu = new JPopupMenu();
          popupMenu.add(onePopupItem);
          popupMenu.add(twoPopupItem);
          popupMenu.add(threePopupItem);
    
          JMenuItem oneItem = new JMenuItem("Item One");
          JMenuItem twoItem = new JMenuItem("Item Two");
          JMenuItem threeItem = new JMenuItem("Item Three");
    
          JMenu oneMenu = new JMenu("Menu One");
          oneMenu.add(oneItem);
          oneMenu.add(twoItem);
          oneMenu.add(threeItem);
    
          JMenuItem fourItem = new JMenuItem("Item Four");
          JMenuItem fiveItem = new JMenuItem("Item Five");
          JMenuItem sixItem = new JMenuItem("Item Six");
    
          JMenu twoMenu = new JMenu("Menu Two");
          twoMenu.add(fourItem);
          twoMenu.add(fiveItem);
          twoMenu.add(sixItem);
    
          final JMenuBar menuBar = new JMenuBar();
          menuBar.add(oneMenu);
          menuBar.add(twoMenu);
    
          MouseListener oneListener = oneMenu.getMouseListeners()[0];
          oneMenu.removeMouseListener(oneListener);
          menuMap.put(oneMenu, oneListener);
          
          MouseListener twoListener = twoMenu.getMouseListeners()[0];
          twoMenu.removeMouseListener(twoListener);
          menuMap.put(twoMenu, twoListener);
    
          final MouseListener customListener = new MouseListener() {
    
             boolean popupShown = false;
    
             public void mouseClicked(MouseEvent e) {
                menuMap.get(e.getComponent()).mouseClicked(e);
             }
    
             public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                   popupMenu.show(menuBar,
                           e.getX() + e.getComponent().getX(), e.getY());
                   popupShown = true;
                } else {
                   menuMap.get(e.getComponent()).mousePressed(e);
                }
             }
    
             public void mouseReleased(MouseEvent e) {
                if (!popupShown) {
                   menuMap.get(e.getComponent()).mouseReleased(e);
                }
             }
    
             public void mouseEntered(MouseEvent e) {
                menuMap.get(e.getComponent()).mouseEntered(e);
             }
    
             public void mouseExited(MouseEvent e) {
                menuMap.get(e.getComponent()).mouseExited(e);
             }
          };
          oneMenu.addMouseListener(customListener);
          twoMenu.addMouseListener(customListener);
    
          JFrame frame = new JFrame("Menus with Popup");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 100);
          frame.setLocationRelativeTo(null);
          frame.setJMenuBar(menuBar);
          frame.setVisible(true);
       }
    }
    Please post back if this isn't quite what you needed.

    db

  11. #11
    andr33i_yo is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks a lot Darryl.Burke!!

    This is exactly what I want.It is realy important for me so I thank you again!


    Andrew.

Similar Threads

  1. various actions in menuitem of popupmenu
    By Peggy in forum Java Applets
    Replies: 0
    Last Post: 12-11-2008, 11:50 AM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

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