Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MouseEvents are not triggered for JMenu

843805Jan 11 2006 — edited Jan 13 2006
I would like to listen for MouseEvents but i'm not sure if it is a bug in java or some lack of knowledge on my side concerning event handling.
I have a simple menu with one mouse listener and seems that mouseReleased and mouseClicked events are not triggered always as i would expect.

if i run the code that has been attached below, than i have the following strange scenarios.

Menu0 is not opened and click on it once:
pressed Menu0
clicked Menu0
There is no mouseReleased received. why?

Menu0 is opened and click on it once again:
pressed Menu0
released Menu0
clicked Menu0
Menu0 is collapsed and i have all the events. ok

Menu0 is opened and click on Menu1:
pressed Menu1
released Menu1
Menu0 is collapsed and there is no mouseClicked event. why?

Menu0 is opened and go over Menu2 and click on it:
pressed Menu2
clicked Menu2
There is no mouseReleased event. why?

Press the mouse over Menu2 and release it over Menu3:
pressed Menu2
released Menu2
why is it 'released Menu2' and not 'released Menu3' if it is pressed and released Menu2 than where is the clicked event anyway.

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MouseEventTest
{
	public static void main( String[] args )
	{
		JFrame frame;
		JMenuBar menuBar;
		JMenu menu, subMenu;
		JMenuItem menuItem;
		MouseListener mouseListener;

		mouseListener = new MouseAdapter() {
			public void mouseEntered( MouseEvent e )
			{}

			public void mouseExited( MouseEvent e )
			{}

			public void mouseClicked( MouseEvent e )
			{
				System.err.println( "clicked " + ((JMenuItem) e.getSource()).getText());
			}

			public void mousePressed( MouseEvent e )
			{
				System.err.println( "pressed " + ((JMenuItem) e.getSource()).getText());
			}

			public void mouseReleased( MouseEvent e )
			{
				System.err.println("released " + ((JMenuItem) e.getSource()).getText());
			}
		};

		menuBar = new JMenuBar();

		menu = new JMenu( "Menu0" );

		menu.addMouseListener( mouseListener );
		menuBar.add( menu );

		menuItem = new JMenuItem( "Menu1" );
		menuItem.addMouseListener( mouseListener );
		menu.add( menuItem );

		subMenu = new JMenu( "Menu2" );
		subMenu.addMouseListener( mouseListener );
		menu.add( subMenu );

		menuItem = new JMenuItem( "Menu3" );
		menuItem.addMouseListener( mouseListener );
		subMenu.add( menuItem );

		menuItem = new JMenuItem( "Menu4" );
		menuItem.addMouseListener( mouseListener );			
		subMenu.add( menuItem );

		frame = new JFrame();
		frame.setTitle( "MouseEvent test for JMenu" );
		frame.setBounds( 320, 240, 320, 240 );
		frame.setJMenuBar( menuBar );
		frame.addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e ) {
				System.exit(0);
			}
		});
		frame.setVisible( true );
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2006
Added on Jan 11 2006
2 comments
140 views