CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2008
    Posts
    61

    [RESOLVED] Menu and popup menu can't use the same menu item?

    Hi, I'm trying to do this: "fileMenu" and "popupMenu" use the same menu item, but it didn't show correctly. When I add redMenuItem to popupMenu, redMenuItem disappears in fileMenu.
    Please help me.
    Thanks.

    Code:
    package gui2;
    
    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.JPopupMenu;
    import javax.swing.JRadioButtonMenuItem;
    
    public class AFrame extends JFrame {
    	private JRadioButtonMenuItem redMenuItem;
    	private JMenu fileMenu;
    	private JMenuBar bar;
    	private JPopupMenu popupMenu;
    	
    	public AFrame()
    	{
    		super("Test");
    		
    		bar = new JMenuBar();
    		fileMenu = new JMenu("File");
    		redMenuItem = new JRadioButtonMenuItem("Red");
    		popupMenu = new JPopupMenu();
    		
    		fileMenu.add(redMenuItem);
    		bar.add(fileMenu);
    		setJMenuBar(bar);
    		
    		popupMenu.add(redMenuItem); // if comment out this line,
    								     // redMenuItem will appear in file menu.
    								   // if uncomment out this line, 
    								    // redMenuItem will disappear in file menu.									   
    		
    		addMouseListener(new MouseAdapter()
    			{
    				public void mousePressed(MouseEvent e) {
    					checkForPopupTrigger(e);
    				}
    
    				public void mouseReleased(MouseEvent e) {
    					checkForPopupTrigger(e);
    				}
    				
    				private void checkForPopupTrigger(MouseEvent e)
    				{
    					if(e.isPopupTrigger())
    						popupMenu.show(e.getComponent(), e.getX(), e.getY());
    				}
    			}
    		);		
    	}
    	
    	public static void main(String[] args) {
    		AFrame app = new AFrame();
    		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		app.setSize(500, 500);
    		app.setLocation(300, 200);
    		app.setVisible(true);
    	}
    }
    Last edited by Emerald214; October 15th, 2009 at 01:20 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Menu and popup menu can't use the same menu item?

    You can't use the same GUI component twice in this way. The best approach is to have two different menu item objects named "Red". If they both do the same thing then assign the same ActionListener (or Action object) to both of them.

  3. #3
    Join Date
    Jun 2008
    Posts
    61

    Re: Menu and popup menu can't use the same menu item?

    Can't really? I have doubt about that because I think it's a reference so it can be used in many places.
    I need one more answer. Anyway, thank you ^^.
    Last edited by Emerald214; October 15th, 2009 at 05:18 AM.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Menu and popup menu can't use the same menu item?

    Quote Originally Posted by Emerald214 View Post
    Can't really? I have doubt about that because I think it's a reference so it can be used in many places. I need one more answer. Anyway, thank you.
    So does it work or not? You asked the question because it didn't work, and you got an answer telling you that you that it won't work and suggesting a working alternative. You want a second opinion?

    Why not take a look at the source code, and trace for yourself what happens when you add a menu item to a menu.

    Teachers open the door, but you must enter by yourself...
    Chinese proverb
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Menu and popup menu can't use the same menu item?

    Quote Originally Posted by Emerald214
    Can't really? I have doubt about that because I think it's a reference so it can be used in many places.
    I need one more answer. Anyway, thank you ^^.
    A polite response would have been to ask why it won't work

    Of course if you had spent a little time thinking about it it would be obvious. How can you have the same component simultaneously displaying in multiple places when components have methods like getLocation(), getParent(), getSize(), hasFocus() etc.

  6. #6
    Join Date
    Jun 2008
    Posts
    61

    Re: [RESOLVED] Menu and popup menu can't use the same menu item?

    I'm very sorry for my poor English. I'm shamed for my response. I wish I could go to the past and do it better.
    Code:
    for(int i = 1; i < 1000; ++i) 
      JOptionPane.showMessageDialog(null, "Sorry");
    Last edited by Emerald214; October 28th, 2009 at 03:38 PM.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: [RESOLVED] Menu and popup menu can't use the same menu item?

    Code:
    for(int i = 1; i < 1000; ++i) 
      JOptionPane.showMessageDialog(null, "Sorry");


    Don't worry we have all written posts that have come across as being more to the point than we intended.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured