Java action listener on menu, and not on menu item?

You can add an ActionListener to a JMenu as this method is inherited from AbstractButton . (JMenu Documentation).

Up vote 1 down vote favorite share g+ share fb share tw.

I need enlightenment. How to add action actionListener event bind to the menu, and not bind to the menu ITEM here is the demo code, that works(on menuITEM).. menuFileItem. AddActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.

Println("It works"); } } ); but when I try the same , but just on the MENU itself it doesn't work! MenuFile. AddActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.

Println("Plz work... :( "); } } ); is it possible to add listener to menu? I taught listener could be added to everything. Java swing menu onclick actionlistener link|improve this question edited Feb 21 at 9:42kleopatra12.9k21325 asked Feb 20 at 9:24?

?266 90% accept rate.

You can add an ActionListener to a JMenu as this method is inherited from AbstractButton. (JMenu Documentation) JMenu menu = new JMenu("MyMenu"); menu. AddActionListener(new ActionListener(){...}); But, it is not intended to be used this way: JMenu ignores ActionEvent.

You should use MenuEvent and MenuListener instead. JMenu menu = new JMenu("MyMenu"); menu. AddMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { System.out.

Println("menuSelected"); } @Override public void menuDeselected(MenuEvent e) { System.out. Println("menuDeselected"); } @Override public void menuCanceled(MenuEvent e) { System.out. Println("menuCanceled"); } }).

I did that, I declaired menu, before constructor, and I created it in constructor of that class, and when I try to implement menu. AddActionListener ..... it doesn't work. Can you provide, working example of your solution to this.

–? Feb 20 at 9:41 You are right, I just tried it myself. Edited my answer.

– TPete Feb 20 at 9:46 WORKS! Thank you! I just got smarter for 1 method... :D joke... thank you really, now my app will look smoother.. I started to work in java few days ago, from scratch .. so i'm in learning phase right now.

THANKS AGAIN MATE! God Bless you! :) –?

Feb 20 at 9:53.

If you instantiated JMenu class to bulid a menu object, try method addMenuLintener().

I never used that method. –? Feb 20 at 9:44 Of course.

Following is the code you can complete it. JMenu menuFileItem= new JMenu(); menuFileItem. AddMenuListener( new MenuListener(){ public void menuSelected(MenuEvent e) { throw new UnsupportedOperationException("Not supported yet.

"); } public void menuDeselected(MenuEvent e) { throw new UnsupportedOperationException("Not supported yet. "); } public void menuCanceled(MenuEvent e) { throw new UnsupportedOperationException("Not supported yet. "); } } ); – zari Feb 20 at 9:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions