Re: JMenu holding JButton instances - bug with 5.0?



On 4/26/2005 at 4:51:36 AM, allen@xxxxxxxxxxxxxxxxx wrote:

> 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.

--
Regards,

John McGrath
.