Displaying a message by right-clicking a GUI button
Michael Payte
Greenhorn
Joined: Sep 14, 2012
Posts: 14
posted
0
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: 32633
4
posted
1
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: 32633
4
posted
0
Moving discussion to our GUIs forum.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32633
4
posted
0
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
posted
0
> 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
posted
0
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: 32633
4
posted
0
It would be all right inside a frame, yes.
But have you worked out how to detect the right button yet?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Displaying a message by right-clicking a GUI button