Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[JMenu]Disabling JMenu

0 views
Skip to first unread message

dmoyne

unread,
Jan 1, 2010, 10:17:28 AM1/1/10
to
I have a JMenu with a collection of JMenuItems ; depending of my app some of
those JMenuItems become disabled one after the other ; at a time all are
disabled ; my question :
- is there any way with a java method to get the number of JMenuItems of my
JMenu still enabled to disable my Jmenu also.

Of course I can :
(1) count them in a loop after each enabling/disabling action,
(2) set a counter for each enabling/disabling action,
and with the count decide what to do by comparing with the total number of
JmenuItem.

Happy new year !

John B. Matthews

unread,
Jan 1, 2010, 10:56:46 PM1/1/10
to
In article <4b3e120f$0$21535$426a...@news.free.fr>,
dmoyne <daniel...@sfr.fr> wrote:

> I have a JMenu with a collection of JMenuItems ; depending of my app
> some of those JMenuItems become disabled one after the other ; at a
> time all are disabled ; my question : - is there any way with a java
> method to get the number of JMenuItems of my JMenu still enabled to
> disable my Jmenu also.
>
> Of course I can :
> (1) count them in a loop after each enabling/disabling action,

Why not? It should be more than fast enough. In this example, the method
highlightDuplicates() checks for duplicates among each of 17 keys after
each key press. Any latency is imperceptible:

<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/
src/org/gcs/robot/RCKeys.java?revision=56&view=markup>

> (2) set a counter for each enabling/disabling action, and with the
> count decide what to do by comparing with the total number of
> JmenuItem.

This seems more fragile and harder to maintain.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

dmoyne

unread,
Jan 2, 2010, 4:31:24 AM1/2/10
to
John B. Matthews wrote:

Thanks John,
so you confirm that there is no buil-in java methods in java classes and
that a hand-made method is necessary.

John B. Matthews

unread,
Jan 2, 2010, 7:32:03 AM1/2/10
to
In article <4b3f126d$0$2265$426a...@news.free.fr>,
dmoyne <daniel...@sfr.fr> wrote:

I can only confirm that I don't know of a built-in method. :-)

markspace

unread,
Jan 2, 2010, 12:49:44 PM1/2/10
to


I'm with John, I know of no automatic way of doing this.

Instead of looping and counting, you may be able to listen for state
changes (a ChangeListener) in the JMenu, which might be easier than
looping. You'll need to account for items that can't be disabled (a
separator, for example).

John B. Matthews

unread,
Jan 2, 2010, 5:13:15 PM1/2/10
to
In article <hho0vs$7mo$1...@news.eternal-september.org>,
markspace <nos...@nowhere.com> wrote:

Capital idea. I'd originally discounted the second approach as perilous
and too reminiscent of reference counting, but updating a Set of menu
items in the listener has some appeal. I'd expect most implementations
of isEmpty() to be very fast.

0 new messages