my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Is there anyway of overriding the default signal for PopupMenus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is there anyway of overriding the default signal for PopupMenus" Watch "Is there anyway of overriding the default signal for PopupMenus" New topic
Author

Is there anyway of overriding the default signal for PopupMenus

Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Guys and Galls,

I am trying to implement the rotation of an object on a canvas. You guys have already helped me with setting up the objects on a canvas and it works fine now. The default for PopupMenus seems to be - on Windows anyway - to wait until the mouse is released. Is there any way to override this as I want the PopupMenu to show when the mouse is rightclicked.

Kind regards,

Apprentice
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

MouseEvent#isPopupTrigger() returns true on right clicks.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Maneesh,

I include the code and you will see what the problem is. The only mouse event that the method isPopupTrigger() responds to is mouseReleased.
The program presents two pop-up menus. One when the canvas is clicked and one when the object on the canvas is clicked. I am trying to achieve the rotating of the clicked object. I think my whole logic may be a little haywire.
I need to rotate the object around by dragging it.

Anyway - code below;

Main class Shaping




The Panel on which the objects are drawn



The MouseListener class - Path Wrangler2


Thanks

The Apprentice
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

Originally posted by Kieran Murray:

The default for PopupMenus seems to be - on Windows anyway - to wait until the mouse is released. Is there any way to override this as I want the PopupMenu to show when the mouse is rightclicked.


I am a bit confused here.
What exactly are you trying to do?

1) Show a popup on right click? Easiest way to do this is to use JComponent#setComponentPopupMenu(). This way you don't have to bother adding listeners, figuring out if the mouse event is a popup trigger and showing the popup.

2) ...show when the mouse is rightclicked. A "click" is technically a combination of a "press"+"release". Now if you want to show the popup on a "press" you can have your mouse listener call a JPopupMenu#show() on mousePressed.
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Maneesh,

Yes. I am not making quite clear what I am trying to do.
If I click on a object such as a Line with a right click I want a pop up menu to comeup which has a Popup MenuItem entitled "Rotate Object". When I click on this MeuuItem I want to then left click on the object and then drag it through a rotation.

The real problem for me is understaning the code in the Popup MenuItem ActionListener to do this.

If I set up a class for each piece of drawing - ie a class for a Line, a class for a particular shape then I presume that I can use the setComponentPopupMenu() on each of the instantiated classes.

Kind regards,

The Apprentice
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear All,

I managed to solve the problem.

Thanks for your help.

Kind regards,

The Apprentice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Originally posted by Kieran Murray:
Dear All,

I managed to solve the problem.
Well done

Please tell us how, for the next time anybody has such a problem.
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Ranchhands,

I have highlighted the relevant code. The MyInterp class and PathWrangler2 class have been changed.

Windows it seems from my reading waits for the MouseReleased before the PopupMenu appears - it is just what it does - irritating but that is Windows - anyway some people use Windows.

When the PopupMenu appears I see the boolean - rotating - to true in the PathWrangler2 class through an ActionListener. This means that when someone next clicks on the object the MousePressed method moves to a certain snippet of the code and rotates the object.

The actual rotating code I took from a Sun forum and just used for my own ends.

I have highlighted the code added.


The Main class - no code changed here



MyInterp code 1 line of code added in the ActionListener lst 4



The code for PathWrangler2 where most of the changes have occured


The rotating does not work well if you do not click back on an object and it whirls too quickly.

Anyway.

Kind regards,

The Apprentice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Originally posted by Kieran Murray:
Dear Ranchhands,

I have highlighted the relevant code. . .
Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there anyway of overriding the default signal for PopupMenus
 
Similar Threads
display 2 popupmenus at the same time
Adding different popupmenus to different jtree nodes
Popupmenus staying popped up
About PopupMenus...
Keeping the Mouse inside a JPanel