Results 1 to 3 of 3
Thread: JMenuItem query
- 05-03-2012, 04:09 AM #1
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
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
internal frame and panel creation as well as a few buttonsJava 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);
Portion of actionPerformed MethodJava 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);
last 2 line should be executed on menuitem clickJava 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);
FYI, I have tried using dotnotation to define a new actionlistenerJava Code:pane.setJMenuBar(menubar); pane.setVisible(true); if (e.getSource().equals(submenu1)){ JOptionPane.showMessageDialog(null, "Your info is saved");}
I get no response on clickJava 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."); } });
Please let me know if I should include any additional code. thanksLast edited by delion19; 05-03-2012 at 04:30 AM. Reason: additional Information
- 05-03-2012, 09:39 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-03-2012, 06:40 PM #3
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
JMenuItem with Java JRE 1.6
By talljames in forum AWT / SwingReplies: 2Last Post: 11-29-2011, 01:18 PM -
JMenuItem Size
By 67726e in forum AWT / SwingReplies: 0Last Post: 09-24-2010, 01:28 AM -
JFrame and JMenuItem
By Kligham in forum AWT / SwingReplies: 10Last Post: 01-08-2010, 02:58 AM -
How to obtain the name of the JMenuItem?
By Azuxard in forum AWT / SwingReplies: 1Last Post: 03-23-2009, 03:33 AM -
More than one KeyStroke (Shortcut) for a JMenuItem
By hannehomuth in forum Advanced JavaReplies: 0Last Post: 07-25-2008, 03:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks