File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes setting Keyboard shortcut for JPopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setting Keyboard shortcut for JPopupMenu" Watch "setting Keyboard shortcut for JPopupMenu" New topic
Author

setting Keyboard shortcut for JPopupMenu

Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
Hi
Can any body know about How to add keyboard shortcut for JPopupMenu.
Like setMnemonic method for JButton.

Thanks in advance


Shashidhar<br /> SCJP 1.5<br /> SCWCD 5.0
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Shashidhar Rao:
Hi
Can any body know about How to add keyboard shortcut for JPopupMenu.
Like setMnemonic method for JButton.

Thanks in advance


I am presuming you are going to use JMenu and JMenuItem instead of JButton.
This is the way its typically done:


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

Joined: Jun 17, 2007
Posts: 175
Hi Maneesh
Thanks for your prompt reply. Actually what my problem is I have used popup menus in my applcation. Some where else client is using this application and he using Mac mouse. It is having only one button. So, he can't able to see popup menu items.
So, if we have shortcut key to open popupmenu it will be easy for him.

I have gone through the API but I didn't found any such method till now. If you know solution for this please let me know..
Thanks in advance.

Shashi
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Shashidhar Rao:
Actually what my problem is I have used popup menus in my applcation. Some where else client is using this application and he using Mac mouse.


Hi Shashi,
My bad. I was under the impression you were talking about the mnemonics and accelerators for the popup.
I personally havent worked on a Mac machine. So my solution might be wrong.

The windows keyboards have a standard button which is mapped to the right click action/context menu/shift+F10 action You can probably follow the same approach. There is a convenient method called createRootPane which you can tweak to get your result. Here is some code:



I had used this to hide1 the dialog when you press the escape key. You can probably decide on some standard MacKeyboard button and use it instead of the escape.
Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
Hi again,
First of all I am not thinking about Mac machine. What I am thinking is How to create keyboard shortcut to open the popupmenu. For suppose I don't want to use mouse in application I want to open the popupmenu by keyboard.
Can you tell how to do this?

Thanks in advance..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Shashidhar Rao:
Hi again,
First of all I am not thinking about Mac machine. What I am thinking is How to create keyboard shortcut to open the popupmenu. For suppose I don't want to use mouse in application I want to open the popupmenu by keyboard.
Can you tell how to do this?

Thanks in advance..


Thats easily done using the popup.show(Component invoker,int x,int y)
You will need to:
1) Override the createRootPane as I mentioned earlier. There you will need to register the key(s) which will trigger the popup
2) This will call a method which should accept the invoker component and the popup to be displayed. For this you will need some kind of mechanism to figure out which component is currently having the focus and the popup menus associated with it
3) You will have to then figure out the x and y coordinates of the currently focused component and then display the popup accordingly.

Hope this helps.
Best of luck.
Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
Thanks, I hope I will get now...
Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
Hi Maneesh again
I registered one key. Then How would we know the key is fired in our code.
So, that I can call particular method.

Please help me in this regard...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Shashidhar Rao:
Hi Maneesh again
I registered one key. Then How would we know the key is fired in our code.
So, that I can call particular method.

Please help me in this regard...


Refer to the createRootPane() I posted. You need to override it in your base frame.
Inside that you register the keys by using


Also you call your method from there itself. My example calls performActionCancel(). Similarly you need to call your method.
 
 
subject: setting Keyboard shortcut for JPopupMenu
 
Similar Threads
code tags in Websphere
WSAD; "run on server" key shortcut ???????
IntelliJ, format code isn't working on Ubuntu
Problem while working with JPopupMenu
question on a wsad keyboard shortcut