Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Event Handler for current menu selection.

 
Enda Mc Laughlin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic