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.