I have written code to perform click event on JMenuItem but that is not firing when I am debugging. (I know that I am not supposed to ask these questions in this forum but I am new to this forum)
public class ClsMenu extends JMenuItem implements ActionListener {
JTextArea output;
JScrollPane scrollPane;
public ClsMenu(String text)
{
super(text);
addActionListener(this);
}
public JMenuBar createMenu()
{
JMenuBar menuBar;
JMenu menuFood,menuDrinks,menuParty;
JMenuItem foodItem;
menuBar=new JMenuBar();
menuFood=new JMenu("Food");
foodItem=new JMenuItem("Pizza");
menuFood.add(foodItem);
menuBar.add(menuFood);
return menuBar;
}
public void createGUIandShow()
{
JFrame frame = new JFrame("Restuarant");
frame.setJMenuBar(createMenu());
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("item clicked"+e.getActionCommand());
}
}
In this call, I have created an object
public class ClsMenuDisp {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClsMenu menu=new ClsMenu("testitem");
menu.createGUIandShow();
}
}
You need to add your ActionListener
to the menu items you are adding the the JMenu
. What you are doing is make a menu item that has a action listener. Then using a instance method of that menu item to create a completely unrelated JMenuBar
/JMenu
/JMenuItem
that is then attached to the JFrame
.