Results 1 to 3 of 3

Thread: JMenuItem query

  1. #1
    delion19 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default JMenuItem query

    Hi all,

    I am relatively new to Java and the forum here. Was wondering if I could get some help with a problem I've been working on for the past few hours. Here's the general idea.

    1. I have 1 main Jframe with an Jpanel that contains many different buttons.
    2. When a button is clicked, a new JDialog is opened using actionPerformed with Jtextareas for input. The dialog also includes a menubar - file and help.
    3. from the dropdown file menu, 3 options (MenuItems) can be selected; save, save & exit or cancel.

    The problem that I am having is with the response when one of these menuitems are selected. I want to be able to execute a particular portion of code on click of that menu item, but have been unable to get a proper response thus far.


    I should also include that the code for these responses has been placed in the same area as the ones for the button clicks.

    A snippet is shown below.

    frame creation
    Java Code:
    JFrame myFrame = new JFrame();
            JDesktopPane desktop = new JDesktopPane();
            JMenuBar menubar = new JMenuBar();
            JMenu menu = new JMenu("File"), menu1 = new JMenu("View"), menu2 = new JMenu("Edit"), submenu;JMenuItem submenu1, submenu2, submenu3;
            menu.setMnemonic(KeyEvent.VK_F);
            menu.getAccessibleContext().setAccessibleDescription("The first Menu with Items");
            menubar.add(menu);menubar.add(menu1);
    internal frame and panel creation as well as a few buttons
    Java Code:
    JInternalFrame internalFrame = new JInternalFrame("Welcome to the "
                    + "Car Dealership Application",true,true,true,true);
            JPanel panel = new JPanel(new FlowLayout());
            myFrame.setSize (framewidth, framelength);
            internalFrame.setSize(framewidth-3, framelength-10);
            myFrame.setVisible(true);
            internalFrame.setVisible(true);
            myFrame.setTitle ("Dealership Application");
            myFrame.setLocation (framelocationx, framelocationy);
            myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
            myFrame.getContentPane().add(panel);
            myFrame.getContentPane().add(desktop);
            
            desktop.add(internalFrame);
    
            JLabel windowLabel = new JLabel ("Please Click on the button the "
                    + "corresponds to the action that you wish to perform");
            internalFrame.add(panel);
            panel.add(windowLabel);
            JButton addNewCar = new JButton ("Add New Car");
            panel.add(addNewCar);
            addNewCar.addActionListener(this);
    Portion of actionPerformed Method
    Java Code:
    public void actionPerformed (ActionEvent e) 
        {
            JButton clickedButton = (JButton) e.getSource();
            //JMenuItem clickedmenuitem = (JMenuItem) e.getSource();
            if(clickedButton.getText().equals("Add New Car"))
            {
               // JOptionPane.showMessageDialog(null, "You are about to add a new Car");
                
                //JPanel panel = new JPanel(new FlowLayout());
                JMenuBar menubar = new JMenuBar();
                JMenu menu = new JMenu("Help"), menu1 = new JMenu("File"), menu2 = new JMenu("Edit"), submenu;JMenuItem submenu1, submenu2, submenu3;
                menubar.add(menu1);menubar.add(menu);            
                menu.addSeparator();
            submenu = new JMenu("Add");
            submenu.setMnemonic(KeyEvent.VK_A);
            
            JMenuItem menuitem = new JMenuItem("New Car",KeyEvent.VK_N);
            menuitem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_N, ActionEvent.ALT_MASK));
            submenu.add(menuitem);
            menuitem = new JMenuItem("Used Car",KeyEvent.VK_U);
            menuitem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_U, ActionEvent.ALT_MASK));
            submenu.add(menuitem);
            menuitem = new JMenuItem("Car Model",KeyEvent.VK_M);
            menuitem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_M, ActionEvent.ALT_MASK));
            submenu.add(menuitem);
            
            menu1.addSeparator();
            submenu1 = new JMenuItem("Save",KeyEvent.VK_S);
            submenu1.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_S, ActionEvent.ALT_MASK));
            submenu2 = new JMenuItem("Save & Exit",KeyEvent.VK_E);
            submenu2.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_E, ActionEvent.ALT_MASK));
            submenu3 = new JMenuItem("Cancel",KeyEvent.VK_A);
            submenu3.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_A, ActionEvent.ALT_MASK));
            
            
            menu1.add(submenu1);
            menu1.add(submenu2);
            menu1.add(submenu3);
            menu.add(submenu); 
    
    JDialog pane = new JDialog();
                pane.setTitle("New Car Menu");
                pane.setSize(framewidth, framelength);
                pane.setLayout(new FlowLayout());
                pane.setModal(true);
                
                JTextField VINtext = new JTextField(VIN,45);
                JTextField colortext = new JTextField(color,45);
                JTextField pricetext = new JTextField(price,45);
                JTextField maketext = new JTextField(make,45);
                JTextField modeltext = new JTextField(model,45);
                JTextField yeartext = new JTextField(yea,45);
                
                JLabel windowLabel = new JLabel ("Please enter the required  "
                    + "information then click <save> or click <cancel> to exit"
                        + " without saving.");
                
                pane.add(windowLabel);
    last 2 line should be executed on menuitem click
    Java Code:
    pane.setJMenuBar(menubar);    
                pane.setVisible(true);
                
    
                    if (e.getSource().equals(submenu1)){
                        
                JOptionPane.showMessageDialog(null, "Your info is saved");}
    FYI, I have tried using dotnotation to define a new actionlistener
    Java Code:
    submenu1.addActionListener(new ActionListener(){
                    public void ActionPerformed (ActionEvent ae){System.out.println("working");}
    
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                });
    I get no response on click

    Please let me know if I should include any additional code. thanks
    Last edited by delion19; 05-03-2012 at 04:30 AM. Reason: additional Information

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: JMenuItem query

    I normally prefer one ActionListener per JMenuItem; the particular ActionListener immediately knows what to do: one of load a file, save a file, exit etc. I don't see that in your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    delion19 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: JMenuItem query

    Quote Originally Posted by JosAH View Post
    I normally prefer one ActionListener per JMenuItem; the particular ActionListener immediately knows what to do: one of load a file, save a file, exit etc.
    Thanks Jos, implemented it in that format and now I have the workable responses - one per JMenuItem. Still working out the kinks to get it right though.

    Thanks

Similar Threads

  1. JMenuItem with Java JRE 1.6
    By talljames in forum AWT / Swing
    Replies: 2
    Last Post: 11-29-2011, 01:18 PM
  2. JMenuItem Size
    By 67726e in forum AWT / Swing
    Replies: 0
    Last Post: 09-24-2010, 01:28 AM
  3. JFrame and JMenuItem
    By Kligham in forum AWT / Swing
    Replies: 10
    Last Post: 01-08-2010, 02:58 AM
  4. How to obtain the name of the JMenuItem?
    By Azuxard in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 03:33 AM
  5. More than one KeyStroke (Shortcut) for a JMenuItem
    By hannehomuth in forum Advanced Java
    Replies: 0
    Last Post: 07-25-2008, 03:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •