> In the past I have had reasons for adding JButton (and JCheckBox)
> instances to a JMenu's popup. This has worked successfully prior to
> 5.0. Now with 5.0 it seems the buttons and checkboxes get stuck when
> clicked, with no indication of what went wrong. They get stuck before
> their action listeners are even called.
Popups have always been a problem area with Swing. The event handling
seems to change with each new Java version, and code that depends on
anything that is not well-documented and commonly used sometimes breaks.
The difficulties have mostly dealt with focus: giving the focus to another
window does not work consistently across platforms and sometimes popups
must be shown in another window. As a result, popup menus do not actually
get the focus; events are delivered to the parent window and then are
retargetted to the popup.
I cannot think of any reason that mouse handling should be affected by
this. However, I suspect that the complexity of the event handling is
the root cause.

Signature
Regards,
John McGrath
> In the past I have had reasons for adding JButton (and JCheckBox)
> instances to a JMenu's popup. This has worked successfully prior to
> 5.0. Now with 5.0 it seems the buttons and checkboxes get stuck when
> clicked, with no indication of what went wrong.
I just traced through this, and I think my first impressions were correct.
It is a royal mess! Here is what I found:
The mousePressed and mouseClicked events get through to the JButton, but
the mouseReleased event does not seem to. As a result, the JButton does
not fire an ActionEvent.
The MenuSelectionManager retargets the mouseReleased event, consuming the
original event and creating a new event that it sends to the JPopupMenu.
However, the JPopupMenu ignores this event.
Since the original event is consumed, you would think it should be
ignored, and under most circumstances, it is. However, for some reason it
only checks if the event has been consumed after it has allowed
InputMethods to process it. Obviously, it needs to check to see if an
InputMethod has consumed the event, but I do not see why it does not do so
under other circumstances.
So what is happening to your mouseReleased event is that it is being
marked as consumed by the MenuSelectionManager, and then the event
processing is stopped after InputMethod processing. If you disable
InputMethods on the buttons (which really does not makes sense anyway), it
continues to process the event, even though it has been consumed().
So if you set enableInputMethods(false) on the buttons (checkboxes, radio
buttons, etc) that you place on the menu, the buttons will work. I really
think it would be a bad idea to depend on this, since it will likely break
in a subsequent Java version, but if you need something that works *now*,
this may be the answer. I would definitely recommend that you get rid of
the use of non-MenuItem components in your menus.

Signature
Regards,
John McGrath
allen@rrsg.ee.uct.ac.za - 29 Apr 2005 08:01 GMT
Hi,
Thank you very much - this is really interesting! I resorted to using
JMenuItems for the JButtons, with a bit of tweaking they can look
better. However this discussion makes me amazed that some of my other
code ever worked to start off with. I've embedded a JTable (in a
scrollpane) using JCheckBox cell-renderers/editors into a JPopupMenu,
and based on your description here, I'm amazed that it worked. It is
sad that there is this potential problem however, since I find that
being able to embed configuration items into a popup menu reduces the
number of levels of configuration dialogs that I would otherwise have
to resort to in order to present all the configuration details to the
user.
Thanks for your time and advice.
Allen
> I just traced through this, and I think my first impressions were correct.
> It is a royal mess! Here is what I found:
[quoted text clipped - 26 lines]
> this may be the answer. I would definitely recommend that you get rid of
> the use of non-MenuItem components in your menus.
John McGrath - 30 Apr 2005 04:52 GMT
> I resorted to using JMenuItems for the JButtons, with a bit of
> tweaking they can look better.
Indeed. JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem do pretty
much the same thing as JButtons, JCheckBoxes, and JRadioButtons, but they
are intended to be used in a menu. So I think they make a much better
starting point. You can always customize them to get the effect that you
want.
> However this discussion makes me amazed that some of my other
> code ever worked to start off with. I've embedded a JTable (in a
> scrollpane) using JCheckBox cell-renderers/editors into a JPopupMenu,
> and based on your description here, I'm amazed that it worked.
Did you place the table in a menu connected to a JMenuBar, or in a plain
JPopupMenu? I am not sure why they are different, but as you have seen,
they are.

Signature
Regards,
John McGrath