I am trying to call an actionListener on a JMenuitem as follows :
class testMenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1);
repaint();
printAll(getGraphics()); //Extort print all content
}
}
public static void main(String args[])
{
Frame frame = new Frame();
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem1,menuItem2,menuItem3;
menuBar = new JMenuBar();
menu = new JMenu("File");
menuItem1 = new JMenuItem("New",
KeyEvent.VK_T);
menuBar.add(menu);
menu.add(menuItem1);
menu = new JMenu("Help");
menuItem2 = new JMenuItem("How to use",
KeyEvent.VK_T);
menu.add(menuItem2);
menuItem3 = new JMenuItem("Link to Paper",
KeyEvent.VK_T);
menu.add(menuItem3);
menuBar.add(menu);
// Calling actionListener here
menuItem1.addActionListener(new testMenuItemListener());
// I do not wish to lose context of the frame here
frame.setJMenuBar(menuBar);
frame.setTitle("Si-AHP");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
Calling so gives me the following error
Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());
^
1 error
I tried converting it into a static method, it however didnt solve the problem. I do not wish to lose context of the frame object. How do I make a successful call, or if not possible, how do I better add a JMenu to a Frame outside the PSVM ?