File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Event Handler for current menu selection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handler for current menu selection." Watch "Event Handler for current menu selection." New topic

Event Handler for current menu selection.

Enda Mc Laughlin

Joined: May 10, 2002
Posts: 4
Hi, the problem is that I want to recognise when the user has highlighted a menu bar using the keyboard. For example, I want an eventhandler to trigger everytime the user pressed alt-f to access the file menu - an eventhandler that is fired every time file is highlighted. I know how to add one for a mouse when the file menu item is pressed. Also, I want to know what the user has highlighted under that menu - not nexessarily clicked on -for example new or close or open etc.
The following code creates a basic menu added to a frame. I intend to the frame by adding all manner of components to it.
Thanks for any help.

import javax.swing.*;
import java.awt.*;
public class GUI
public GUI ()
public void FrameSetUp()
JFrame MainFrame = new JFrame();
GridLayout grid1;
JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu ("File");
JMenuItem New_Item;
JMenuItem Open_Item;
JMenuItem Close_Item;
JMenuItem Copy_Item;
New_Item = new JMenuItem ("Set Player One Name");
NewHandler Menu_New = new NewHandler();
New_Item.addActionListener( Menu_New );
file.add (New_Item);
Open_Item = new JMenuItem ("Open");

Close_Item = new JMenuItem ("Close");
file.add (Close_Item);
jmb.add (file);
JMenu edit = new JMenu ("Edit");
Copy_Item = new JMenuItem ("Copy");
edit.add (Copy_Item);
Copy_Item.setHorizontalTextPosition (JMenuItem.LEFT);
jmb.add (edit);

System.out.println("Mainframes height "+MainFrame.getHeight());
System.out.println("Mainframes width "+MainFrame.getWidth());

private class NewHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
System.out.println("Pressed new menu.\n");

}//end event handler
}//end Button1 handler...

public static void main(String[] args)
GUI MainGUI = new GUI();
catch (Exception e)

} //end of main GUI class :roll:
Enda Mc Laughlin

Joined: May 10, 2002
Posts: 4
Sorry just to clarify - this problem applies to when a menu or menu item is highlighted using the keyboard or the mouse. Again, thanks for any help.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You would have to use a FocusListner to determine when the MenuItem has gained focus from either the keyboard or the mouse, and an ActionListener to determine if it has actually been fired( this can happen either from the keyboard or the mouse ).


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Event Handler for current menu selection.
jQuery in Action, 3rd edition