aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Keystroke? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Keystroke?" Watch "Keystroke?" New topic
Author

Keystroke?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

I would like to have a keystroke within my menuItem for File Open so that when I select Ctrl + O the listener for openmenu runs.
Is it possible to do so retaining the anonymous ActionListener(), or, do I have to create an Action or ActionListener objects?

I had also come across the following code snippet, but it does not seem to work:




Related links
1. How to use Actions
2. InputMap
3. InputMap


Jon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Try looking for key bindings in the Java™ tutorials. That would be the first step.
I shall move this discussion to our GUIs forum.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
Read the Swing tutorial on How to Use Menus for working examples of setting mnemonics and accelerators.

The code you posted is NOT a SSCCE. Your question is about invoking a menu item. The actual code in your ActionListener is irrelevant. All you need is a System.out.println(...) to prove the menu item gets invoked. We don't need to see all your MouseListeners or code to open the file.

Keep the SSCCE simple if you want people to read the code.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Rob Camick wrote:Read the Swing tutorial on How to Use Menus for working examples of setting mnemonics and accelerators.

The code you posted is NOT a SSCCE. Your question is about invoking a menu item. The actual code in your ActionListener is irrelevant. All you need is a System.out.println(...) to prove the menu item gets invoked. We don't need to see all your MouseListeners or code to open the file.

Keep the SSCCE simple if you want people to read the code.


I removed the rest of the code...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
I removed the rest of the code...


One line of code doesn't help us. That is NOT what I suggested. That is NOT a SSCCE.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Rob Camick wrote:
I removed the rest of the code...


One line of code doesn't help us. That is NOT a SSCCE.


It does not help me either because I don't know, what do you expect me to come up with from 'I have no idea'?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

Jon Camilleri wrote:
It does not help me either because I don't know, what do you expect me to come up with from 'I have no idea'?

We expect you to click on the words SSCCE to see what SSCCE really means


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

Joined: Jun 13, 2009
Posts: 1788
    
    2
I don't know, what do you expect me to come up with from 'I have no idea'?


And that is your problem. You don't read or listen to the suggestions. You don't know what problem you are trying to solve.

First of all your were given a section to read in the Swing tutorial. Since it has a working example on how to use accelerators you should have to the answer to your question and this posting should be closed.

If however you still have a problem then you need to post a SSCCE. Your question is about a menu item so for your SSCCE you need:

a) a JFrame
b) a JMenuBar
c) a JMenu
d) a JMenItem
e) an ActionListener (that displays a message when clicked)

That is all you need. So the program will be maybe 20-30 lines maximum:

- A couple of lines to create and show a frame.
- A couple of lines to create and add a menuber to the frame.
- A couple of lines to create and add a menu to the menu bar.
- A couple of lines to create and add a menu to the menubar.
- A couple of lines to create and add a menu item to the menu.
- A couple of lines to create and add the ActionListener to the menu item.

Start from scratch. Forget about your existing program. This way its easier to find silly mistakes since you are not reading through 100's of lines of unnecessary code.

Until you understand this simple concpet, I can't help you with your answers. Problem solving is a skill you need to develop.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Keystroke?
 
Similar Threads
IS there fake components to add to flow layout so I can create fake space?
Smiley
check if setEditable is defined as a method
SwingWorker causes problem
Parsinng HTML