Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-22-2010, 06:13 PM
Member
 
Join Date: Jun 2008
Posts: 77
Rep Power: 0
ravjot28 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-23-2010, 10:37 AM
Member
 
Join Date: Nov 2009
Posts: 52
Rep Power: 0
hannes is on a distinguished road
Default
I think you have to add a mouselistener
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-23-2010, 12:30 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,388
Rep Power: 11
Fubarable is on a distinguished road
Default
or perhaps you just want to use a tool tip instead.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-23-2010, 05:32 PM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 266
Rep Power: 2
zweibieren is on a distinguished road
Default
Here's how I created a context menu for a text field. Should work just as well for a button.

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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove “Input method” submenu selection from StyledText context menu? Mitja SWT / JFace 0 09-21-2009 11:33 AM
How to remove Eclipse context menu items? Neil_ Eclipse 0 04-06-2009 05:09 AM
SWT JFace bug using context menu xcallmejudasx SWT / JFace 0 11-14-2008 04:04 PM
Fill a menu dynamically when menu is shown Java Tip SWT 0 07-07-2008 04:47 PM
how to create Popup Menu with Sub Menu while right-clicking the JTree Node?? Kabiraa AWT / Swing 7 05-09-2008 07:54 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org