Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm currently developing a version 2

Status
Not open for further replies.

cwinans

Programmer
Mar 26, 2001
102
0
0
US
I'm currently developing a version of MineSweeper in Java for my Special Topics course at Penn State. I've seen lots of versions out there... none utilizing the full benefits of Java and it's object oriented abilities. I've written the underlying model for a truly object-oriented version. I'm now working on the view of the game. I can't seem to find any information on detecting right mouse clicks. I've search high and low for sources that I could study and possibly use or improve upon... nothing but JavaScript examples galore.

Is there anyone who knows how to determine if a right mouse button has clicked on a component? I'm using swing if that matters but any ideas that work strictly in AWT let me know and I'll study them to get the gist of them.

Thanks a bunch in advance!!!!





I hope this helped! ;-)
- Casey Winans
 
Hi Casey,

Here is a _very_ simple example, but I think you get the idea from it.

import javax.swing.*;
import java.awt.event.*;

public class MListener extends JFrame implements MouseListener
{
public static void main(String[] argv)
{
new MListener();
}

public MListener()
{
this.addMouseListener(this);
this.setSize(100,100);
this.show();
}

public void mouseClicked(MouseEvent e)
{
// BUTTON1_MASK == left button
// BUTTON2_MASK == _middle_ button
if(e.getModifiers() == InputEvent.BUTTON3_MASK)
{
System.out.println("Right mouse button pressed!");
}
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mousePressed(MouseEvent e)
{
}

public void mouseReleased(MouseEvent e)
{
}
}

For more information about java.awt.event package, see java API documentation from (worth of downloading from sun's pages)

-Vepo
 
You can do something along these lines:

Component gameBoard = new Component(); //you know....


gameBoard.addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){

if (SwingUtilities.isRightMouseButton(e)){
//Do you work here....
}
}
});

This will do what you want. ------
KJR
 
Thank you both for your help. I have already figured it out using the MouseListener.. but now someone else in the future will have good examples to look at. Thanks a bunch.

I'll put a link to the game in another post when I get it done. It's due the middle of December and I love to program so I started early. I hope this helped! ;-)
- Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top