//See if the code suit you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IntroExample extends JMenuBar {
String[ ] fileItems = new String[ ] { "New", "Open", "Save", "Exit" };
String[ ] editItems = new String[ ] { "Undo", "Cut", "Copy", "Paste" };
char[ ] fileShortcuts = { 'N','O','S','X' }; //_N
char[ ] editShortcuts = { 'Z','X','C','V' }; //control z
final JMenu fileMenu;
public IntroExample() {
fileMenu = new JMenu("File"

;
JMenu editMenu = new JMenu("Edit"

;
JMenu otherMenu = new JMenu("Other"

;
// Assemble the File menus with mnemonics.
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand( ) +
"] was pressed."

;
}
};
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems
, fileShortcuts);
item.addActionListener(printListener);
fileMenu.add(item);
}
// Assemble the File menus with keyboard accelerators.
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts,
Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
item.addActionListener(printListener);
editMenu.add(item);
//if (i==3) item.setDisplayedMnemonicIndex(-1);
}
// Insert a separator in the Edit menu in Position 1 after "Undo".
editMenu.insertSeparator(1);
// Assemble the submenus of the Other menu.
JMenuItem item;
// Assemble the Other menu itself.
otherMenu.add(item = new JCheckBoxMenuItem("Check Me"
);
item.addActionListener(printListener);
otherMenu.addSeparator( );
ButtonGroup buttonGroup = new ButtonGroup( );
otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"
);
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"
);
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator( );
otherMenu.add(item = new JMenuItem("Potted Plant",
new ImageIcon("image.gif"
));
item.addActionListener(printListener);
// Finally, add all the menus to the menu bar.
add(fileMenu);
add(editMenu);
add(otherMenu);
}
public static void main(String s[ ]) {
JFrame frame = new JFrame("Simple Menu Example"
;
frame.getContentPane().setLayout(null);
frame.setBounds(new Rectangle(0, 0, 200, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final IntroExample myIntro = new IntroExample();
frame.setJMenuBar(myIntro);
//frame.getContentPane().setLayout(null);
final menuFocus menuFocusObj = new menuFocus(false);
JPanel jPan = new JPanel();
JPanel jPan2 = new JPanel();
jPan.setBounds(0,0,200,250);
//jPan.setBackground(Color.white);
frame.getContentPane().add(jPan);
jPan.setLayout(new GridLayout(4,1));
//jPan2.setLayout(new GridLayout(1,2));
JTextField jText1 = new JTextField("Text1"
;
JTextField jText2 = new JTextField("Text2"
;
JTextField jText3 = new JTextField("Text3"
;
//JLabel checkLabel = new JLabel("Tap to menu"
;
jPan.add(jText1);
jPan.add(jText2);
jPan.add(jText3);
jPan.add(jPan2);
//jPan2.add(checkLabel);
final JCheckBox chBox = new JCheckBox("Tap to menu",false);
chBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
if (source==chBox)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
menuFocusObj.setFlag(true);
}
else
{
menuFocusObj.setFlag(false);
}
}
}
}
);
chBox.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
if (menuFocusObj.getFlag()==true)
(myIntro.fileMenu).doClick();
}
}
);
jPan2.add(chBox);
frame.setVisible(true);
(myIntro.fileMenu).doClick();
frame.repaint();
}
}
class menuFocus
{
private boolean enabled;
public menuFocus(boolean temp)
{
enabled = temp;
}
public void setFlag(boolean temp1)
{
enabled = temp1;
}
public boolean getFlag()
{
return enabled;
}
}
//Some object are final because I have to access it within Anonymous class
//Please Mark this post as helpful if the code suits you.