// I'm trying to write a program that reads a log file. // I created this menu, but I now have to be able to // determine when the start time and end time are // selected. The e.getActionCommand().equals("LABEL")) // tells me when a button with that LABEL was press, // but I have four buttons with the same label under // different menus. How do I determine what button // was pressed from which menu.
public class MainWindow extends JFrame implements WindowListener, ActionListener {
private JMenuBar menuBar = new JMenuBar(); // Window menu bar String[] fileItems = new String[] { "New", "Save", "Print", "Exit" }; String[] tenthItems = new String[] { "00", "10", "20", "30", "40", "50" }; String[] unitItems = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; char[] fileShortcuts = { 'N','S','P','X' };
public MainWindow() { super("PTC DATA REDUCTION"); addWindowListener(this);
/* * Menu begin */ setJMenuBar(menuBar);
/* * Makes File menu and Submenu */ JMenu fileMenu = new JMenu("File"); JMenu startMenu = new JMenu("Start Time"); JMenu startMinSubMenu = new JMenu("Minutes"); JMenu startSecSubMenu = new JMenu("Seconds"); JMenu endMenu = new JMenu("End Time"); JMenu endMinSubMenu = new JMenu("Minutes"); JMenu endSecSubMenu = new JMenu("Seconds"); JMenu helpMenu = new JMenu("Help");
/* * Assemble the File menus with keyboard accelerators */ for (int i=0; i < fileItems.length; i++) { JMenuItem item = new JMenuItem(fileItems, fileShortcuts); item.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts, java.awt.Event.CTRL_MASK, false)); item.addActionListener(this); fileMenu.add(item); }
/* * Assemble the submenus of the Start Time Menu for minutes */ ButtonGroup startMinTypes = new ButtonGroup(); for (int i=0; i < tenthItems.length; i++) { JMenu itemTenth = new JMenu(tenthItems); for (int x=0; x < unitItems.length; x++) { JMenuItem itemUnit = new JRadioButtonMenuItem(unitItems[x]); itemUnit.addActionListener(this); itemTenth.add(itemUnit); startMinTypes.add(itemUnit); } startMinSubMenu.add(itemTenth); } startMenu.add(startMinSubMenu);
/* * Assemble the submenus of the Start Time Menu for seconds */ ButtonGroup startSecTypes = new ButtonGroup(); for (int i=0; i < tenthItems.length; i++) { JMenu itemTenth = new JMenu(tenthItems); for (int x=0; x < unitItems.length; x++) { JMenuItem itemUnit = new JRadioButtonMenuItem(unitItems[x]); itemUnit.addActionListener(this); itemTenth.add(itemUnit); startSecTypes.add(itemUnit); } startSecSubMenu.add(itemTenth); } startMenu.add(startSecSubMenu);
/* * Assemble the submenus of the End Time Menu for minutes */ ButtonGroup endMinTypes = new ButtonGroup(); for (int i=0; i < tenthItems.length; i++) { JMenu itemTenth = new JMenu(tenthItems); for (int x=0; x < unitItems.length; x++) { JMenuItem itemUnit = new JRadioButtonMenuItem(unitItems[x]); itemUnit.addActionListener(this); itemTenth.add(itemUnit); endMinTypes.add(itemUnit); } endMinSubMenu.add(itemTenth); } endMenu.add(endMinSubMenu);
/* * Assemble the submenus of the End Time Menu for seconds */ ButtonGroup endSecTypes = new ButtonGroup(); for (int i=0; i < tenthItems.length; i++) { JMenu itemTenth = new JMenu(tenthItems); for (int x=0; x < unitItems.length; x++) { JMenuItem itemUnit = new JRadioButtonMenuItem(unitItems[x]); itemUnit.addActionListener(this); itemTenth.add(itemUnit); endSecTypes.add(itemUnit); } endSecSubMenu.add(itemTenth); } endMenu.add(endSecSubMenu);
/* * Assemble the submenus of the Help menu */ JMenuItem subHelpItem = new JMenuItem("Instructions"); subHelpItem.addActionListener(this); helpMenu.add(subHelpItem); helpMenu.addSeparator(); subHelpItem = new JMenuItem("About"); subHelpItem.addActionListener(this); helpMenu.add(subHelpItem);
/* * Finally, add all the menus to the menubar */ menuBar.add(fileMenu); menuBar.add(startMenu); menuBar.add(endMenu); menuBar.add(Box.createHorizontalGlue()); menuBar.add(helpMenu); } public void actionPerformed(ActionEvent e) { Object source; source = e.getSource(); if (source instanceof JMenuItem) { if(e.getActionCommand().equals("New")){ System.out.println("Got New menu item"); } else if(e.getActionCommand().equals("Save")){ System.out.println("Got Save menu item"); } else if(e.getActionCommand().equals("Print")){ System.out.println("Got Print menu item"); } else if(e.getActionCommand().equals("Exit")){ System.out.println("Got EXIT menu item"); } else if(e.getActionCommand().equals("c")){ System.out.println("Got Start Time menu item"); } else if(e.getActionCommand().equals("Instructions")){ System.out.println("Got Instructions menu item"); } else if(e.getActionCommand().equals("About")){ System.out.println("Got About menu item"); } else{ System.out.println("Menu item [" + e.getActionCommand() + "] was pressed."); } } }
/* * Listener interface must implement, not used */ public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { dispose(); // Release window resources System.exit(0); } public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } /java
Instead of using a general actionPerformed method, why don't you implements a listener for each one of the menu items. In this way you will be able to do an specific action for each component instead using a general method.
Juilo I'm lost. I have implement a listener but I get the same results.
/* * Assemble the submenus of the Start Time Menu for minutes. * Register a listener for the radio buttons. */ ButtonGroup startMinTypes = new ButtonGroup();
for (int i=0; i < tenthItems.length; i++) { JMenu itemTenth = new JMenu(tenthItems); for (int x=0; x < unitItems.length; x++) { JMenuItem itemUnit = new JRadioButtonMenuItem(unitItems[x]); RadioListener myListener = new RadioListener(); itemUnit.addActionListener(myListener); itemTenth.add(itemUnit); startMinTypes.add(itemUnit); } startMinSubMenu.add(itemTenth); } startMenu.add(startMinSubMenu);
class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Bill Menu item [" + e.getActionCommand() + "] was pressed."); } }