Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

ActionListeners don't work on JMenu objects, why?

Thread view: 
Aidan - 09 Jun 2005 17:36 GMT
I have a JMenu entitled "New Article", which may or may not have child
JMenuItems to create prepopulated articles of certain types. The user
still needs to be able to select "New Article" to go into a Wizard to
create an article.

So my "File" menu looks like the following:

File
-----------
New Article -> Of Type A
           -> Of Type B
           -> Of Type C

Search Articles
Close Article
Exit

I thought this would have been easy, but JMenu objects do not fire
actionPerformed on listeners.

So how do I do it?
SPG - 09 Jun 2005 17:46 GMT
>I have a JMenu entitled "New Article", which may or may not have child
> JMenuItems to create prepopulated articles of certain types. The user
[quoted text clipped - 17 lines]
>
> So how do I do it?

Hi,

Have you tried adding a MenuListener to the JMenu, then reacting to the
selected/deselected events?

Not sure if this is the same as clicking, but worth a go..
Otherwise I have seen people use hibrids of panels and labels.. There is a
very recent post in this NG about that...

Steve
jonck@vanderkogel.net - 09 Jun 2005 17:47 GMT
JMenuItems fire actionPerforrmed just fine, the problem you're having
must lie elsewhere. If you post your non-functional code I'm sure
someone here will spot the problem in no time at all.

Kind regards, Jonck
Aidan - 09 Jun 2005 18:00 GMT
Steve, yes I did try that, and the MenuActions are sent when the mouse
moves over the menu, but not when it is clicked on, so no go there.

Jonck, this is a JMenu I'm asking about, not JMenuItem.
Lee Weiner - 09 Jun 2005 23:09 GMT
>Steve, yes I did try that, and the MenuActions are sent when the mouse
>moves over the menu, but not when it is clicked on, so no go there.

I don't understand.  I've used MenuListener and I just tested it, it fires
when the Menu is clicked on, not when the mouse movement highlights it.  As
you can see below, it's used to know whether to enable Copy and Cut when the
Edit menu is clicked.

 class TopMenuHandler implements MenuListener   {
   public void menuSelected( MenuEvent e )  {
       System.out.println( "Selected" );
       if (txtArea.getSelectionStart() == txtArea.getSelectionEnd())  {
         mnuCopy.setEnabled(false);
         mnuCut.setEnabled(false);
       }
       else    {
         mnuCopy.setEnabled(true);
         mnuCut.setEnabled(true);
       }
   }
   public void menuDeselected( MenuEvent e ) {}
   public void menuCanceled( MenuEvent e ) {}
 }

Lee Weiner
lee AT leeweiner DOT org
Aidan - 09 Jun 2005 23:24 GMT
Lee - Is your listener on a JMenu or JMenuItem ?
Lee Weiner - 10 Jun 2005 00:17 GMT
>Lee - Is your listener on a JMenu or JMenuItem ?

                                             ///Edit Menu
   TopMenuHandler hnd = new TopMenuHandler();
   JMenu mnuEdit = new JMenu( "Edit" );
   mnuEdit.setMnemonic( 'E' );
   mnuEdit.addMenuListener(hnd);
   menu.add(mnuEdit);

Any questions?

Lee Weiner
lee AT leeweiner DOT org
Roland - 10 Jun 2005 04:25 GMT
>>Lee - Is your listener on a JMenu or JMenuItem ?
>
[quoted text clipped - 9 lines]
> Lee Weiner
> lee AT leeweiner DOT org

Your mnuEdit is probably a top level menu, i.e. it's added to the menu
bar (menu instancof JMenuBar).
When you add a MenuListener to a *sub* menu, its handlers (menuSelected
etc) get fired when the mouse hovers over it, at least in the default
Swing look-and-feel. IIRC in the Motif LnF, a submenu only activates
(expands) when it is clicked on. Swing LnF immediately expands a submenu
when the mouse hovers over it. [And a toplevel menu expands when clicked
on by the mouse.]
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Aidan - 10 Jun 2005 04:57 GMT
Thanks Roland, that's good to know.
Aidan - 09 Jun 2005 18:02 GMT
I should mention that I have somewhat provided a workaround by using a
MouseAdapter and listening on MouseClicks, but it's just a yucky hack.
Michael Rauscher - 09 Jun 2005 22:00 GMT
Aidan schrieb:
> I have a JMenu entitled "New Article", which may or may not have child
> JMenuItems to create prepopulated articles of certain types. The user
[quoted text clipped - 17 lines]
>
> So how do I do it?

Why don't you simply reorganize your menu structure a bit?

New -> Article...
       Article/Type A
       Article/Type B
       Article/Type C
Search Articles
     ...

Or perhaps

New...
New By Type -> Type A
               Type B

Or

New -> Article...
       By Type -> Type A
                  Type B

Personally, it would disturb me if I'd see a submenu that actually
behaves like a menu item.

Bye
Michael
Aidan - 09 Jun 2005 22:22 GMT
I agree that it is slightly unconventional, but I'd like to know _why_
this isn't allowed specifically.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.