Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a message by right-clicking a GUI button

 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving discussion to our GUIs forum.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> display a message by right-clicking a button

MouseEvent.BUTTON2 is the scroll button (if the mouse has one)
 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be all right inside a frame, yes.

But have you worked out how to detect the right button yet?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic