help with GUI assign a keystroke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #1

    help with GUI assign a keystroke

    I'm trying to figure out how to assign a keystroke and a click to a menu item.
    Example If a user click on a menu and selects the Close the program
    Menu and chose Close[/b]. I want the program to open (//inner class Section used for exit on menu 3 for fun) ie "Exit Program".

    here is my code so far.


    Code:
    public class GUIMenu extends JFrame {
     
        public GUIMenu() {
     
            super("FeedBar 2");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
         //Build third menu in the menu bar.
            JMenuItem a1 = new JMenuItem("Close");
            a1.setMnemonic(KeyEvent.VK_D);
     
    //Create the menu bar.
            JMenuBar menubar = new JMenuBar();
     
       //Build the Third menu.
            JMenu menu3 = new JMenu("Close the program");
            menu3.add(a1);
            
            menubar.add(menu3);
     
            // prepare user interface
            JTextArea edit = new JTextArea(8, 40);
            JScrollPane scroll = new JScrollPane(edit);
            BorderLayout bord = new BorderLayout();
            setLayout(bord);
     
            add("Center", scroll);
            setJMenuBar(menubar);
            pack();
            setVisible(true);
        }
     
    	//inner class Section used for exit on menu 3 for fun
        class exitListener implements ActionListener {
            public void actionPerformed(ActionEvent arg0) {
     
                    int x = JOptionPane.showOptionDialog(GUIMenu.this, "Exit Program?",
                                    "Exit Request", JOptionPane.YES_NO_OPTION,
                                    JOptionPane.QUESTION_MESSAGE, null, null,
                                    JOptionPane.NO_OPTION);
                    if (x == JOptionPane.YES_OPTION) {
                    	GUIMenu.this.dispose();
                    }//close the if statement
            }//close public void actionPerformed
    }//close the class exitListener
     
     
        public static void main(String[] arguments) {
            GUIMenu frame = new GUIMenu();
        }
    }
    Thanks
    nomad
    PS I know I need add a listener of some type but I don't know where or what kind.
Working...