Java right mouse button click

Mouse right click in Java

Here’s a small example. You can use the getButton() function from MouseEvent .

final JLabel lblRightClickMe = new JLabel("Right click me"); //create a label lblRightClickMe.setBounds(152, 119, 94, 14); final JPopupMenu jpm = new JPopupMenu("Hello"); //create a JPopupMenu jpm.add("Right");jpm.add("Clicked");jpm.add("On"); //add some elements jpm.add("This");jpm.add("Label"); lblRightClickMe.setComponentPopupMenu(jpm); //set jpm as …

All 5 Replies

  • add MouseListener to container (JPanel. )
  • override public void mouseClicked(MouseEvent e) then there are two ways
panel.addMouseListener(new MouseAdapter() < @Override public void mouseClicked(MouseEvent e) < if (e.getModifiers() == MouseEvent.BUTTON3_MASK && e.getClickCount() == 1) < // whatever >> >); 
 panel.addMouseListener(new MouseAdapter() < @Override public void mouseClicked(MouseEvent e) < if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) < // whatever >> >); 

i think you have to use mouseadapter class and then you have to use mouseclicked event to handle right click of mouse.

final JLabel lblRightClickMe = new JLabel("Right click me"); //create a label lblRightClickMe.setBounds(152, 119, 94, 14); final JPopupMenu jpm = new JPopupMenu("Hello"); //create a JPopupMenu jpm.add("Right");jpm.add("Clicked");jpm.add("On"); //add some elements jpm.add("This");jpm.add("Label"); lblRightClickMe.setComponentPopupMenu(jpm); //set jpm as this label's popup menu lblRightClickMe.addMouseListener(new MouseAdapter() < @Override public void mouseClicked(MouseEvent arg0) < if (arg0.getButton() == MouseEvent.BUTTON3)< //get the mouse button jpm.show(arg0.getComponent(), arg0.getX(), arg0.getY());//set the position and show the popup menu >> >); 

Источник

Right-clicking a Button or JButton

Chrome

send pies

posted 14 years ago

  • Report post to moderator
  • 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?

    Marshal

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Marshal

    send pies

    posted 14 years ago

  • Report post to moderator
  • Are Buttons designed to have a right-click? You usually use right-click on Panels or similar, rather than Buttons.

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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?

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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).

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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 ]

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • Ok cool, thanks for help guys. Now all I got to do is program it. 🙄

    Marshal

    send pies

    posted 14 years ago

  • Report post to moderator
  • Originally posted by Olivier Legat:
    Now all I got to do is program it.

    Thank you, and same to you .

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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?

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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.

    Chrome

    send pies

    posted 14 years ago

  • Report post to moderator
  • 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 :

    Источник

    Mouse Right Click Example Java

    This Java code on Mouse Right Click Example explains how to handle the right clicks of mouse key.

    Mouse Right Click Example
    import java.awt.*; import java.awt.event.*; public class Demo extends Frame < Button btn; public Demo() < setLayout(new FlowLayout()); btn = new Button("OK"); btn.addMouseListener(new MyListener()); add(btn); setSize(300,300); setVisible(true); >public static void main(String args[]) < new Demo(); >> class MyListener extends MouseAdapter < public void mouseClicked (MouseEvent e) < // write here your event handling code if (e.getModifiers() == MouseEvent.BUTTON3_MASK) < System.out.println("You right clicked on the button"); >> >

    Mouse Right Click Example

    Output screen on Mouse Right Click Example

    Other way of event handling is:

    if(e.isMetaDown())
    System.out.println(“You right clicked on the button”);
    >

    The static constant modifier MouseEvent.BUTTON3_MASK or the method isMetaDown() of MouseEvent differentiates the right clicks.

    Instead of button btn, it can be any GUI component like Checkbox etc. Just register the MouseListener or MouseAdapter to a GUI component you would like.

    We extended MouseAdapter to override only one method mouseClicked (MouseEvent e) and if MosueListener is implemented, it is required to override all the five methods.

    Also learn the handling of double and triple clicks of mouse key.

    Do you know how to do the following?

    Источник

    Читайте также:  Arraylist java найти значение
    Оцените статью