CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 15th, 2009, 02:02 AM
    Emerald214 Emerald214 is offline
    Member
     
    Join Date: Jun 2008
    Posts: 55
    Emerald214 is an unknown quantity at this point (<10)
    [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 02:20 AM.
    Reply With Quote
      #2    
    Old October 15th, 2009, 03:18 AM
    keang keang is offline
    Elite Member
     
    Join Date: May 2006
    Location: UK
    Posts: 2,180
    keang is a jewel in the rough (200+) keang is a jewel in the rough (200+) keang is a jewel in the rough (200+)
    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.
    Reply With Quote
      #3    
    Old October 15th, 2009, 05:56 AM
    Emerald214 Emerald214 is offline
    Member
     
    Join Date: Jun 2008
    Posts: 55
    Emerald214 is an unknown quantity at this point (<10)
    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 06:18 AM.
    Reply With Quote
      #4    
    Old October 15th, 2009, 06:27 AM
    dlorde dlorde is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 1999
    Location: UK
    Posts: 9,372
    dlorde is a glorious beacon of light (400+) dlorde is a glorious beacon of light (400+) dlorde is a glorious beacon of light (400+) dlorde is a glorious beacon of light (400+) dlorde is a glorious beacon of light (400+) dlorde is a glorious beacon of light (400+)
    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]...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
    Reply With Quote
      #5    
    Old October 17th, 2009, 07:03 AM
    keang keang is offline
    Elite Member
     
    Join Date: May 2006
    Location: UK
    Posts: 2,180
    keang is a jewel in the rough (200+) keang is a jewel in the rough (200+) keang is a jewel in the rough (200+)
    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.
    Reply With Quote
      #6    
    Old October 28th, 2009, 04:22 PM
    Emerald214 Emerald214 is offline
    Member
     
    Join Date: Jun 2008
    Posts: 55
    Emerald214 is an unknown quantity at this point (<10)
    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 04:38 PM.
    Reply With Quote
      #7    
    Old October 28th, 2009, 05:09 PM
    keang keang is offline
    Elite Member
     
    Join Date: May 2006
    Location: UK
    Posts: 2,180
    keang is a jewel in the rough (200+) keang is a jewel in the rough (200+) keang is a jewel in the rough (200+)
    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.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:42 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.