d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Do U Use Right Click In Swing
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 3 2012 08:32am
Hi im making a GUI in netbeans where I use swing (not awt).

I want something diffenrent to happen when i rightclick (or middleclick) on a button.

Should I somehow include in the actionperformed event?

Currently im handling everything in mouseclicked.

I can use rightclick. But as mentioned I need the GUI to perform another task when using right mouseclick.

Hope U can help :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 3 2012 08:34am
Right click is MouseEvent.BUTTON3 iirc.
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 3 2012 08:48am
isnt that an AWT command ?

Im trying to keep it in swing. Using the palette in netbeans.

Is there any events useable in the IDE or should I write it myself ?

This post was edited by tigeranden on Sep 3 2012 08:48am
Member
Posts: 1,238
Joined: Jul 29 2009
Gold: 7,875.00
Sep 5 2012 06:20am
edit - nope.

If you're serious about using SWING and it's full IDE and avoiding ambiguity then you can use SwingUtilites as such:

SwingUtilities.isRightMouseButton(MoseEvent e)

Which will return a Boolean.

This post was edited by Janitor on Sep 5 2012 06:21am
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 8 2012 09:48am
thank you all.

I still have a problem though.

What am i doing wrong here:

private void jSlider1MouseClicked(java.awt.event.MouseEvent evt) {


if(SwingUtilities.isLeftMouseButton(evt)==true);
System.out.print("left");


if(SwingUtilities.isRightMouseButton(evt)==true);
System.out.print(" rigth ");


if(SwingUtilities.isMiddleMouseButton(evt)==true);
System.out.print("Middle");

}

whether I left, right or midlleclick I always get the result "left right middle" :/
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 8 2012 10:38am
Quote (tigeranden @ Sep 8 2012 11:48am)
thank you all.

I still have a problem though.

What am i doing wrong here:

    private void jSlider1MouseClicked(java.awt.event.MouseEvent evt) {                                     


        if(SwingUtilities.isLeftMouseButton(evt)==true);
        System.out.print("left");
   

        if(SwingUtilities.isRightMouseButton(evt)==true);
        System.out.print(" rigth ");


        if(SwingUtilities.isMiddleMouseButton(evt)==true);
        System.out.print("Middle");

    } 

whether I left, right or midlleclick I always get the result "left right middle" :/


thats because all your if statements end with semi colons.

if you're using eclipse, right click the project, go to properties, then click the plus sign next to compiler settings, errors/warnings. one of them is something like "empty statement" and set it to "error". then these three will produce compile-time errors and tell you that the statement does nothing.

This post was edited by carteblanche on Sep 8 2012 10:39am
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 8 2012 10:50am
ups is my face red.

Ty for help. :)
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 8 2012 11:56am
in Netbeans the slider moves one unit in the direction of the mouse even before I wrote any eventhandlers.

need the slider to snap to the position of the mouse when left-clicking.

when right clicking the slider should move to the right. the amount is calculated by t.getbb.

When middle-clicking the mouse should move the same amonut to the left (t.getbb)

note. that right-clicking allways means adding value nomatter where the user clicks relative to the current position of the slider.
Left-clicking always means subtracting value nomatter where the user clicks relative to the current position of the slider.

So need to disable the default add one or subtract one whether right or left to the current value of slider.

This is how my jSlider1MouseClicked lookes now.

name your price if u think u can write the whole method. but still any help is appriciated off course.

private void jSlider1MouseClicked(java.awt.event.MouseEvent evt) {


if(SwingUtilities.isLeftMouseButton(evt)==true)
jSlider1.setValue(jSlider1.getMousePosition().x*3/2);//had to add the *3/2 for it to look kinda nice. No idea why!

if(SwingUtilities.isRightMouseButton(evt)==true)
jSlider1.setValue(jSlider1.getValue()+(int)t.getbb());

if(SwingUtilities.isMiddleMouseButton(evt)==true)
jSlider1.setValue(jSlider1.getValue()-(int)t.getbb());


}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll