Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411990 Posts in 69441 Topics- by 58486 Members - Latest Member: Fuimus

June 17, 2024, 12:30:39 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[SOLVED]Differentiating between left and right mouse clicks in Java
Pages: [1]
Print
Author Topic: [SOLVED]Differentiating between left and right mouse clicks in Java  (Read 1529 times)
imaginationac
Level 2
**


Makin' games instead of makin' money.


View Profile WWW
« on: January 03, 2009, 05:49:57 PM »

Okay, so this has been driving me nuts, but I can't seem to figure out how to handle the events for a left mouse click and right mouse click differently in Java.

Here's a snippet of the code I currently have set up.
Code: (Java)
    public void mouseClicked(MouseEvent e) {
        // Get the source first
        Object source = e.getSource();
        // Get which mouseButton was clicked.
        int buttonType = e.getButton();
        if (source instanceof Cell) {
            Cell cell = (Cell) source;
            int r = cell.getRow();
            int c = cell.getCol();
            //System.out.println(buttonType);
            // Left click
            if (buttonType == MouseEvent.BUTTON1);
            {
                //If the game hasn't started yet
                if (game == null) {
                    game = new MineSweeper();
                    game.startNewGame(9, 9, 10);
                }
                game.printBoard();
                game.uncoverCell(r, c);
                game.printBoard();
                System.out.println("Left mouse button clicked.");
                this.updateGUI();
                game.checkWinCondition();
            }
            // Right click.
            if (buttonType == MouseEvent.BUTTON3) {
                System.out.println("Right mouse button clicked.");
            }
        }
    }

You can take a look at the entire project at the link in my signature.

What's happening right now is when ever I right click on an object, a left click event is fired as well.

Am I missing something, obvious or not? Is there a better way to do this? I would very much appreciate the help!
« Last Edit: January 03, 2009, 11:27:53 PM by imaginationac » Logged

Youtube channel | Charger! Dev Log
              
Bad Sector
Level 3
***


View Profile WWW
« Reply #1 on: January 03, 2009, 07:44:44 PM »

Try to add
Code:
if (e.getID() != MouseEvent.MOUSE_PRESSED) return;
a MouseListener listens for several events, not only clicking. Usually when you "click" the MOUSE_PRESSED, MOUSE_RELEASED and MOUSE_CLICKED events are fired in that order. Since you don't check the event's ID, your code is called three times.
Logged

~bs~
imaginationac
Level 2
**


Makin' games instead of makin' money.


View Profile WWW
« Reply #2 on: January 03, 2009, 09:15:46 PM »

I'm not sure that's right. I know what you described happens, but the overridden methods for mousePressed and mouseReleased are blank because I don't need to watch for those events. I only need to know when a button is clicked (as described in the API), and to know which one it is.

EDIT: Solved it.  I had an extra semi-colon.  Embarrassed
« Last Edit: January 03, 2009, 11:26:16 PM by imaginationac » Logged

Youtube channel | Charger! Dev Log
              
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic