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 / April 2005

Tip: Looking for answers? Try searching our database.

JMenu holding JButton instances - bug with 5.0?

Thread view: 
allen@rrsg.ee.uct.ac.za - 26 Apr 2005 09:51 GMT
Hi,
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. Does anyone have an idea why
this might be happening? Below is some basic test code to demonstrate
the problem (demo code only!). Notice how the buttons in the JPopupMenu
work fine, but the buttons in the JMenu get stuck.

thanks

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

/**
* Test Menus and popups for use with other components.
*/

public class MenuTest extends JFrame
{
    private JPopupMenu popup = new JPopupMenu( );
    private JMenu menu = new JMenu( "TopMenu" );

    public MenuTest( )
    {
        super( "MenuTest" );
        JMenuBar menuBar = new JMenuBar( );
        setJMenuBar( menuBar );
        menuBar.add( menu );

        init( popup );
        init( menu.getPopupMenu( ) );

        final JPanel pCenter = new JPanel( );
        add( pCenter, BorderLayout.CENTER );
        setSize( 300, 300 );
        setDefaultCloseOperation( EXIT_ON_CLOSE );

        pCenter.addMouseListener( new MouseAdapter( )
        {
            public void mouseClicked(MouseEvent e)
            {
                if ( SwingUtilities.isRightMouseButton( e ) )
                {
                    popup.show( pCenter, e.getPoint( ).x, e.getPoint( ).y );
                }
            }
        } );
    }

    private void init( Container c )
    {
        c.add( new JMenuItem( "MenuItem" ) );
        c.add( new JButton( "JButton" ) );
        c.add( new JCheckBox( "JCheckBox" ) );
    }

    public static void main( String[] args )
    {
        MenuTest frame = new MenuTest( );
        frame.setVisible( true );
    }
}
John McGrath - 28 Apr 2005 20:23 GMT
> 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

John McGrath - 28 Apr 2005 22:06 GMT
> 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



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.