Results 1 to 11 of 11
  1. #1
    lime is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Checking of pressing right and left mouse button not working properly

    Hi, i need checkout if i pressed left, right or both mouse buttons using SwingUtilities class, here's my code:
    if (SwingUtilities.isLeftMouseButton(e) | SwingUtilities.isRightMouseButton(e)) {
    //not important
    } else if (SwingUtilities.isLeftMouseButton(e)) {
    //not important
    } else if (SwingUtilities.isRightMouseButton(e))
    //not important
    -if i press both buttons (maybe within a half second) it's working fine - it's doing what i want, the problem is when i press left or right button alone, program do the code like if i pressed the both buttons. I can do it other way but i need to do it with SwingUtilities class, thanks for help.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Checking of pressing right and left mouse button not working properly

    The problem is with your logic. Change your first statement to use && instead of |. Otherwise, the first statement will always evaluate to true if one or the other (or both) button is pressed.

    Regards,
    Jim
    Last edited by jim829; 04-24-2014 at 04:05 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    lime is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Checking of pressing right and left mouse button not working properly

    Well i tried it before, but if i used this:
    if (SwingUtilities.isLeftMouseButton(e) && SwingUtilities.isRightMouseButton(e))
    -it never worked like if i pressed both buttons, always left or right one..

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Checking of pressing right and left mouse button not working properly

    It should be ||. A button cannot be both left and right, unless you're unlucky enough to work on a Mac :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    lime is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Checking of pressing right and left mouse button not working properly

    No it's not working too, should i have to do a simple example then you can try it? If i used || i couldn't press a single button

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Checking of pressing right and left mouse button not working properly

    Oh wait. Jeebus... I should have read more properly. Jim is spot on with && if you need to test for both, if that's even possible.... I'll try and see if it can be done with both buttons...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  7. #7
    lime is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Checking of pressing right and left mouse button not working properly

    I think it's not possible to check it with && because i couldn't press the both buttons in the same time with it, here is the example, you can try it:
    package sandbox;

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class Sandbox extends JFrame {

    public static void main(String[] args) {
    Sandbox s = new Sandbox();
    s.setVisible(true);

    }

    public Sandbox() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300, 200);
    setResizable(false);
    setTitle("Frame");
    setLayout(null);
    JButton but = new JButton("click me");
    but.setBounds(0, 0, 120, 50);
    but.addMouseListener(new MouseListener() {

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    if(SwingUtilities.isLeftMouseButton(e) | SwingUtilities.isRightMouseButton(e))
    System.out.println("both");
    else if(SwingUtilities.isLeftMouseButton(e))
    System.out.println("left");
    else if(SwingUtilities.isRightMouseButton(e))
    System.out.println("right");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    });
    add(but);
    }

    }
    //i need to use SwingUtilities and MouseListener!

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Checking of pressing right and left mouse button not working properly

    Ah, now I see... It's a trick question. Testing both buttons at once is NOT possible, but it can be done.

    I am not going to give you the answer straightaway, since I believe this is a school assignment. But your prof sure made it hard to guess...

    Don't bother testing both buttons at the same time, because Swing doesn't understand that anyway. Keep track with two boolean variables of the state of both buttons in mousePressed(MouseEvent) and mouseReleased(MouseEvent). If the left mousebutton is pressed, set that variable to true, when released set it to false. Then you can check if both variables are true... That's the bottom line.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: Checking of pressing right and left mouse button not working properly

    Actually I'd keep track of press and release using two booleans per button.

    press - set upon press
    release - set upon release

    And then when both press and release are true at the same time, you know if the button was quickly tapped in between measuring moments. Perhaps that is useless information, perhaps it is not. In any case I'd not overwrite the state upon release, because then you don't ever see quick taps as presses.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Checking of pressing right and left mouse button not working properly

    Quote Originally Posted by gimbal2 View Post
    Actually I'd keep track of press and release using two booleans per button.

    press - set upon press
    release - set upon release

    And then when both press and release are true at the same time, you know if the button was quickly tapped in between measuring moments. Perhaps that is useless information, perhaps it is not. In any case I'd not overwrite the state upon release, because then you don't ever see quick taps as presses.
    How can you have both states pressed and released at the same time? And you'd have to reset the press variable to false when it's released otherwise it represents the wrong state, which basically makes both variables eachothers counterparts all the time.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: Checking of pressing right and left mouse button not working properly

    As I say: by quick tapping so you press-release within one "measuring moment". Then you get a press and a release event right after each other and so the release event will cancel out the press event before the application gets a chance to do something with it.

    Granted: that depends on the design of the application if it is a problem or not.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. pressing a button making a instance in another class
    By SpicyElectricity in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 10:48 PM
  2. Browse Button Not Working Properly
    By Chinmayananthan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-29-2011, 07:15 AM
  3. Replies: 1
    Last Post: 08-03-2011, 09:41 AM
  4. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  5. Menu item not working properly for mouse events
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •