The moose likes Swing / AWT / SWT / JFace and the fly likes Right-clicking a Button or JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Right-clicking a Button or JButton" Watch "Right-clicking a Button or JButton" New topic
Author

Right-clicking a Button or JButton

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Hello everyone,

Just a simple question, is it possible in Java to have a right-click on a Button perform a task? It probably is, but I have no idea how to that. If it is though I need to know how to do this. Could anyone give me a hint on where to start (a link to a tutorial or to a Java API class will do). I usually use ActionListener for Left-clicking I assume I can't use that interface. Could I use a MouseListener?


Olly
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 25024
If you go through the MouseEvent class, you find you can get the different buttons, so you can have choices depending on which button was clicked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 25024
Are Buttons designed to have a right-click? You usually use right-click on Panels or similar, rather than Buttons.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 109
Originally posted by Campbell Ritchie:
Are Buttons designed to have a right-click? You usually use right-click on Panels or similar, rather than Buttons.


Yeah, noo they aren't. Buttons are used for actions to show/do something, you don't put popup menu for button and it has no preferences through secondary mouse button.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Buttons are used for actions to show/do something, you don't put popup menu for button and it has no preferences through secondary mouse button.


Hmm, that could be annoying. Because you see I'm actually trying to make a replicate of Minesweeper. And so I want a flag to be placed on the button when the user right-clicks it. Does that mean I'll have to right-click some other sort of Component instead Of Button or JButton. Are you sure there's no way to do it with a JButton, using the addMouseListener method?
Michael Dunn
Rancher

Joined: Jun 09, 2003
Posts: 3600
add your mouseListener to the button, and in mousePressed(MouseEvent me)
if(SwingUtilities.isRightMouseButton(me))//check the buttons icon, setting the appropriate one

you could also use
if(me.isMetaDown())...
if(me.getModifiers() == 4)...
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 109
Originally posted by Olivier Legat:


Hmm, that could be annoying. Because you see I'm actually trying to make a replicate of Minesweeper. And so I want a flag to be placed on the button when the user right-clicks it. Does that mean I'll have to right-click some other sort of Component instead Of Button or JButton. Are you sure there's no way to do it with a JButton, using the addMouseListener method?



Ouki douki I was thinking desktop design in my post, not game. There is a difference.



edit:
And happy new year for you all where ever you think tou are
[ December 31, 2008: Message edited by: Mikko Kohtam�ki ]
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Ok cool, thanks for help guys. Now all I got to do is program it. :roll:

Happy New Year
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 25024
Originally posted by Olivier Legat:
Now all I got to do is program it.

Have a look at the Java Tutorials.

Happy New Year

Thank you, and same to you .
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 852
Olivier Legat wrote:you see I'm actually trying to make a replicate of Minesweeper. And so I want a flag to be placed on the button when the user right-clicks it. Does that mean I'll have to right-click some other sort of Component instead Of Button or JButton.


I wrote my own port of minesweeper a few years back, and the right-click thing was a problem.

I ended up writing a custom component extending JButton that would fire differently on left
and right clicks. I extended DefaultButtonModel also, because I wanted it to work correctly
in cases where the user clicks, drags-off-button, either drags back on to button or not, and
lets go. It was actually somewhat complicated.

It ended up working pretty well, but I never got it working completely to my satisfaction
because I wanted it to also work on single-button macs (where control-left emulates right)
and I was having trouble getting this part to work consistently across all JDKs.


bitguru blog
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

I wrote my own port of minesweeper a few years back, and the right-click thing was a problem.


Strangely enough the right-click thing wasn't the biggest problem for me. Using the MouseAdapter I was able to implement the left right and middle mouse buttons very easily. But well, I think this is a new feature of the JDK 1.6. I haven't actually tested it on a Mac so I might still have that same problem as you.
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15020

I don't see why you don't just use a JLabel. Style it to look like a button and use a MouseListener to deal with click events, changing the labels icon as you go.


My Blog | DZone Articles
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Gregg Bolinger wrote:I don't see why you don't just use a JLabel.


Well yes I could have and it would have worked just as well. But really I find that it makes more sense to use a Button or JButton because technically the game is based on buttons. I ended up using a JButton because I could add an ImageIcon to them to enhance the appearance. I could have used the Button class but then I wouldn't be able to add pictures to them (very inconvenient especially for the flags and mines). Whereas with a JLabel you don't have click sensation when you pressed it (I mean as in it doesn't seem like a real button). I could have of course tuned it up to have a same sensation a JButton but then well... who wants to do extra work?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 852
Olivier Legat wrote:Strangely enough the right-click thing wasn't the biggest problem for me. Using the MouseAdapter I was able to implement the left right and middle mouse buttons very easily.


Well, I was trying to do it "right."

By that I mean that if the user right-presses on the button, moves the cursor outside the boundary of the button, then releases the button that no event should be fired. But if the user right-presses on the button, moves the cursor outside the boundary of the button, moves the cursor back on to the button's interior, then releases the button that the event should be fired. The appearance of the button should change when the mouse is dragged over the button's boundary like this. Also, it should handle things correctly when the user changes button mid-click (for example: left-press, right-press, left-release, right-release). I handled most of this stuff in the custom ButtonModel.

As I said, I got it all working except for getting control-left to emulate right across all JDKs. The problem was the JDKs were inconsistent in what events I would get.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Brian Cole wrote:

By that I mean that if the user right-presses on the button, moves the cursor outside the boundary of the button, then releases the button that no event should be fired.



I see what you mean but it's a different concerning Minesweeper. Note that right-clicking places a flag on a square. If you play Minesweeper of Windows Vista you should realize that a flag is placed the moment the right button is clicked (it doesn't wait for it to be released). This is for the obvious reason that flagging isn't as vital as left-clicking (because you might land on a mine, so should allow the user to make a "last-second cancel" sort of option). Whereas for flagging you can just remove the flag is a mistake is made, this also allows to the user to play faster. The main problem with my code though is that when user left-clicks+hold and then moves the mouse anywhere within/outside the button and then releases nothing happens (even if the cursor is over the button). However is the user left-clicks and doesn't move mouse at all until the button is released, then the action is performed.

Maybe you could help me fix this :



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 7059

Ok.
A possible short cut/workaround. Yeah! I am lazy.
I tried out the minesweeper after many years.
On my version (XP) right click sets a flag/question mark/clears the button.
Can be achieved by setting a popup.


[Donate a pint, save a life!] [How to ask questions]
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 166

Maneesh Godbole wrote:Ok.
On my version (XP) right click sets a flag/question mark/clears the button.


I am aware of this question mark but I'm not programming it because I personally hate it.
 
 
subject: Right-clicking a Button or JButton
 
developer file tools