*
The moose likes Swing / AWT / SWT and the fly likes Displaying a message by right-clicking a GUI button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying a message by right-clicking a GUI button" Watch "Displaying a message by right-clicking a GUI button" New topic
Author

Displaying a message by right-clicking a GUI button

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
I'm trying to make it so that my application will display a message by right-clicking a button (the message must be displayed where the right-click event is detected). However, when I right-click on the buttons, nothing happens. Help would be appreciated; thank you in advance! Here is my complete code (130-163 contains the mouse listener code. mousePressed, mouseEntered, mouseReleased, and mouseExited are intentionally left empty):

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
I would suggest those colours should be chosen from an array.
something.setColor(myColourArray[i++ % myColourArray.length]);
That will of course go horribly wrong if … this happens.

You may possibly have seen that message before; it was in response to some code surprisingly similar to yours.

Go through the Java Tutorials about events. An action event does not depend on a mouse key, but on the GUI button being pushed. Try going through the ActionEvents which are associated with that action, and see whether you get different mask numbers from it. If you read about ActionEvent you see you can get different numbers from it. See whether you get something like ALT_MASK or META_MASK from the event. Note if those are masks, the easiest way to check for them is this sort of thing, which you can try for right middle and left buttons:-

You can tell whether the numbers are used as masks; they will be something like 1, 2, 4, 8, 16, 32, and you can find that from the constant values link.

If that doesn’t help, try coming back and considering a mouse event.

If you can work out which mask number is used, you can change the appropriate if to show your dropdown list.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Moving discussion to our GUIs forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
By the way, if that program is an applet rather than a frame. shouldn’t the code go in the init and paint methods rather than the constructor? I haven’t written applets for ages, so I can’t remember, but that doesn’t look right to me.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> display a message by right-clicking a button

MouseEvent.BUTTON2 is the scroll button (if the mouse has one)
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
Campbell Ritchie wrote:By the way, if that program is an applet rather than a frame. shouldn’t the code go in the init and paint methods rather than the constructor? I haven’t written applets for ages, so I can’t remember, but that doesn’t look right to me.
Actually, I will be changing the JApplet to JFrame. I just haven't done that yet.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
It would be all right inside a frame, yes.

But have you worked out how to detect the right button yet?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Displaying a message by right-clicking a GUI button
 
Similar Threads
How is a Graphical object move on the Screen by using JButton.
requestFocusInWindow() doesnt work
Cycling through multiple colors for a button by pressing it multiple times.
How to make popup invisible
How to use menu event