Results 1 to 4 of 4

Thread: Context Menu

  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default Context Menu

    Hi

    I have various buttons on my frame and i want to add a context menu for each button on right click but i have already an action listener on those buttons
    So how can i add the context menu on right click

    Help Me Please

  2. #2
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I think you have to add a mouselistener

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Here's how I created a context menu for a text field. Should work just as well for a button.

    Java Code:
    		final String demoText
    				= "Fred's flight objection fixes and zaps the wavy bump quickly.";
    		Action resetAction = new AbstractAction("Reset") {
    			public void actionPerformed(ActionEvent e) {
    				text.setText(demoText);
    			}
    		};
    		final JPopupMenu resetMenu = new JPopupMenu();
    		resetMenu.add(new JMenuItem(resetAction));
    
    		
    		text = new JTextField(demoText);
    		text.addActionListener(this);
    		text.addMouseListener(new MouseListener() {
    			void popup(MouseEvent e) {
                                resetMenu.show(text, e.getX(), e.getY());
    			}
    			public void mousePressed(MouseEvent e) 
    					{ if(e.isPopupTrigger()) popup(e); }
    
    			public void mouseReleased(MouseEvent e) 
    					{ if(e.isPopupTrigger()) popup(e); }
    
    			public void mouseClicked(MouseEvent e) {}
    			public void mouseEntered(MouseEvent e) {}
    			public void mouseExited(MouseEvent e) {}
    		} );

    Code is supposed to check isPopupTrigger for both mousePressed and mouseReleased because platforms differ as to which is the trigger.

Similar Threads

  1. Replies: 0
    Last Post: 09-21-2009, 11:33 AM
  2. How to remove Eclipse context menu items?
    By Neil_ in forum Eclipse
    Replies: 0
    Last Post: 04-06-2009, 05:09 AM
  3. SWT JFace bug using context menu
    By xcallmejudasx in forum SWT / JFace
    Replies: 0
    Last Post: 11-14-2008, 04:04 PM
  4. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:47 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
  •