• Post Reply Bookmark Topic Watch Topic
  • New Topic

Popupmenu  RSS feed

 
Sam Hendrickx
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way to connect the popupmenu to the right Object (JFrame). (Something like SwingUtilities.getWindowAncestor(popupmenu)
I've created 2 JFrames each with the same MouseClass.
When you click on the RMB a pop menu is shown with a background item. When clicked on the background item only the last created JFrame changes his background.
Below is my simplified code. I'm probably not thinking like a good OO'er.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
In olden days you would have to
1) Add a mouse listener to the source
2) On mouse click check if the click was the popup trigger (right click on windows)
3) If yes, show the JPopupMenu

However since 1.5, you can achieve all this with one line
JComponent#setComponentPopupMenu(JPopupMenu popup)
From the API docs

Sets the JPopupMenu for this JComponent. The UI is responsible for registering bindings and adding the necessary listeners such that the JPopupMenu will be shown at the appropriate time. When the JPopupMenu is shown depends upon the look and feel: some may show it on a mouse event, some may enable a key binding.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!