Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JButton and JMenuItem need to share same action

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Group!

Ok I have a JButton that opens a new frame/window and I also have  MenuItem that needs to call the same action as well. I am stuck it works one way and not the other. Below is my code and thanks in advance! - Scott


Now my button1 action listener which works fine.

This works. Now I would like to call this same action from a JMenu Item. Now I know how to make the menu and add menu items. How ever the actionListener code will not execute. My menu code is blow in case I did something wrong.

Thanks for the help! So to re-iterate my question. The JButton and JMenuItem need to call the same Action Listener.
 
Bartender
Posts: 4688
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Scott,

both a JButton and a JMenuItem take an Action as parameter in the constructor. You can get such an Action by subclassing an AbstractAction, where you have to override the actionPerformed method.
You can set properties, like icons and other properties, with the method action.putValue(...), and you can enable/disable your button and menu item in one go by using the setEnabled method. It does take some practise to make a good use of an Action, in my experience. See the API.

Another way, in which you only share the actionListener, is to code
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Eric Catalano wrote:So to re-iterate my question. The JButton and JMenuItem need to call the same Action Listener.



You can do something like this:Then they will both invoke the same listener no matter where you place them.


However that's probably not the way I would do it. I would usually create an instantiation of javax.swing.Action and pass it to the JButton and JMenuItem constructors:This is slightly more work but can be cleaner. (ActionExample)
 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that code compile without event -> ?
Another way to do it is to create an action listener reference:-
 
Brian Cole
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Does that code compile without event -> ?


Are you asking about my code or Mr. Souris's code?

My code needs something to replace the "...". It could be a call to a constructor of a named class, or an anonymous subclass, or something like "event -> someMethod()" (as you suggest).

The method reference expressions Mr. Souris uses should be fine, presuming you have java8 or later. They are more or less interchangable with lambdas.
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Cole wrote:. . . Are you asking about my code or Mr. Souris's code?

PS' code.

. . . The method reference expressions Mr. Souris uses should be fine . . .

So it will compile without event -> then.
 
Piet Souris
Bartender
Posts: 4688
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. May I say Brian, and of course you may call me Piet (Mr Souris, although very polite) makes me feel even older than I already feel)    

The only thing with method references is that you must realize what parameters are involved, if any.

the actionPerformed method takes an ActionEvent as parameter. So you can say:

But then the processClick method must be defined with parmeter ActionEvent:

This is also possible:

But now the method should have no parameters:

Disadvantage is that you  don't have access to the ActionEvent.
You have all the liberty to do something wild:

and have

With a method reference, you do not have this liberty. Java knows that there is one parameter, ActionEvent, so the method processClick must be like:

and you can add the actionListener in two ways:

or as usual with methodreferences, let Java take care of this parameter stuff like I did:

In this last case, if your processButtonClick is like this:

you get a compiler error.

Strangely enough, NetBeans gives no error! Only when you run this code do you get an error about uncompilable code!

 
Rancher
Posts: 3248
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree with Brian's suggestion to use an Action (not an ActionListener).

The benefits of using an Action is that it can be shared with multiple components and if a property of the Action is changed it affects all components.

For example if you disable the Action it will disable the menu item and the button.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the suggestions! What would I need to change to make this work based on the code I provided above? The btn1.addActionListener contains my action code which works with the button..does not work with the menu item. One frame has the button code and another frame has the menu code.
 
Piet Souris
Bartender
Posts: 4688
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian gave a link to his book, where Actions are described. A simple example that I used somewhere is:

I create some Actions as follows:

And I populate some panel with

In your case, having such an Action, just create a JButton and a JMenuItem with:

If you happen to use an IDE, you can get an overview of the properties that can be set, by typing 'Action.'  and then a dialog shows up with al the possibilities.
 
Rob Camick
Rancher
Posts: 3248
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on How to Use Actions.

You can download and play with the working examples and then restructure your code to be similiar to the demo code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic