Help GUI select and keystroke

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

    #1

    Help GUI select and keystroke

    I need help with JcomboBox
    When and User select the Exit in the File box it will the
    //inner class
    class exitListener implements ActionListener {
    ...
    }
    which it open a window asking them if they want to close the program.

    I have a statement
    filemenu.setMne monic(KeyEvent. VK_X);
    But the setMnemonic(Key Event .VK_X) is wrong.
    Also I want to be able to drag down and select Exit.
    Can someone please help me on this.



    Here is my code...

    Code:
    class MyFrame extends JFrame {
    	String[] file = { "New", "Open", "Exit" };//items for file
        String[] edit = { "Cut", "Copy", "Paste" };//items for edit
        JComboBox filemenu = new JComboBox();
        JComboBox editmenu = new JComboBox();
     
     
    	public MyFrame(String title) {
    		super(title);
    		this.setSize(250, 250); //sets the size for the frame
    		this.setLocation(200, 200);//location where frame is at
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		// setup contents
    		makeComponents();
    		initComponents();
    		buildGUI();
     
    		// display
    		this.setVisible(true);
    	}
     
    	private void makeComponents() {
     
    		JPanel pane = new JPanel();
    //		file menu section
    		filemenu = new JComboBox();
            JLabel fileLabel = new JLabel();
            pane.add(fileLabel);
            for (int i = 0; i < file.length; i++)
            	filemenu.addItem(file[i]);
            pane.add(filemenu);
            add(pane);
            setVisible(true);
     
    //edit menu section
            editmenu = new JComboBox();
            JLabel editLabel = new JLabel();
            pane.add(editLabel);
            for (int i = 0; i < edit.length; i++)
            	editmenu.addItem(edit[i]);
            pane.add(editmenu);
            add(pane);
            setVisible(true);
    	}
     
    	private void initComponents() {
     
    		filemenu.addActionListener(new exitListener());
    	}
     
     
    	//inner class
        class exitListener implements ActionListener {
            public void actionPerformed(ActionEvent arg0) {
     
                    int x = JOptionPane.showOptionDialog(MyFrame.this, "Exit Program?",
                                    "Exit Request", JOptionPane.YES_NO_OPTION,
                                    JOptionPane.QUESTION_MESSAGE, null, null,
                                    JOptionPane.NO_OPTION);
                    if (x == JOptionPane.YES_OPTION) {
                            MyFrame.this.dispose();
                    }
     
            }
    }
     
     
     
     
    	private void buildGUI() {
    		Container cont = this.getContentPane();// set gui components into the frame
    		this.setLayout(new FlowLayout(FlowLayout.LEFT));// Comp are added to the frame
    		cont.add(filemenu);
    		cont.add(editmenu);
    	}
     
    	// / inner classes
     
    }
     
     
     
    public class ButtonFrame {
     
    	public static void main(String[] args) {
     
    		MyFrame f1 = new MyFrame("This is my Project for GUI");
    	}
     
    }


    Thanks
    nomad
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your filemenu isn't a menu at all, it's a JCombobox. Have a look at the JMenu
    and [b/JMenuItem[/b] classes; they make up the parts of your drop down menus.
    Those classes implement the mnemonics methods and a bit more.

    kind regards,

    Jos

    Comment

    Working...