Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes addActionListener problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "addActionListener problem" Watch "addActionListener problem" New topic
Author

addActionListener problem

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
hello guys,

ive used a JMenu object and cant find the problem when i tried to implement or apply an addActionListener on it..

//insert a menu
//Where the GUI is created:
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;


//Create the menu bar.
menuBar = new JMenuBar();

//Build the first menu. Settings Menu
menu = new JMenu("Settings");
menu.setMnemonic(KeyEvent.VK_S);
menu.getAccessibleContext().setAccessibleDescription("Clock Settings");
menuBar.add(menu);

//Date and Time Menu
menuItem = new JMenuItem("Date and Time",KeyEvent.VK_D);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menuItem.setActionCommand("Date and Time");
menu.add(menuItem);

//set a separator
menu.addSeparator();

//Alarm Menu
menuItem = new JMenuItem("Alarm", KeyEvent.VK_A);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menuItem.setActionCommand("Alarm");
menu.add(menuItem);



//Build the second menu. Exit Menu
menu = new JMenu("Exit");
menu.setMnemonic(KeyEvent.VK_X);
menu.addActionListener(this);
menu.setActionCommand("Exit");
menuBar.add(menu);



when i tried to click on the Exit button, nothing works..

public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("Date and Time")){
System.out.println("Date");
}
else if(ae.getActionCommand().equals("Alarm")){
System.out.println("Alarm");
}
else if(ae.getActionCommand().equals("Exit")){
System.out.println("Exit");
}

}

ive also done this.. i've used the println methods just to see if it works.

thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
ActionListeners usually don't work too well on JMenus. Try adding a JMenuItem to a menu and adding the ActionListener to it for the exit. Many apps have the exit item under a file menu.
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
hmmm.. is this a bug.. i can see some application menu have their exit with the main menu bar..

anyway. will do that..

thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
i can see some application menu have their exit with the main menu bar
It's been quite a while since I tried to do this. If you have seen it done there must be a way; I could have missed something.
 
 
subject: addActionListener problem
 
Similar Threads
Adding Components in to JInternalFrame
how to enable/disable this item?
Adding JMenuBar, after user Authentication.. Please Help
adding an InternalFrameListener to a JMenu Item
JTextArea append() not working?