• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keystroke?  RSS feed

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!