aspose file tools*
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
Author

Event Handler for current menu selection.

Enda Mc Laughlin
Greenhorn

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 ()
{
FrameSetUp();
}
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;
file.setMnemonic('F');
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");
file.add(Open_Item);

file.addSeparator();
Close_Item = new JMenuItem ("Close");
file.add (Close_Item);
jmb.add (file);
JMenu edit = new JMenu ("Edit");
edit.setMnemonic('E');
Copy_Item = new JMenuItem ("Copy");
edit.add (Copy_Item);
Copy_Item.setHorizontalTextPosition (JMenuItem.LEFT);
jmb.add (edit);
MainFrame.setJMenuBar(jmb);

MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.pack();
MainFrame.setVisible(true);
System.out.println("Mainframes height "+MainFrame.getHeight());
System.out.println("Mainframes width "+MainFrame.getWidth());
MainFrame=null;
}

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)
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
GUI MainGUI = new GUI();
}//endtry
catch (Exception e)
{
System.out.println(e.toString());
}//endcatch
}

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

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.
Enda
Nathan Pruett
Bartender

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

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Event Handler for current menu selection.