0

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 ?

siddu
  • 11
  • 5
  • 2
    The solution is to get most of that code out of your main method and into the instance realm where it belongs. Your static main method should be short, and should serve to create your GUI object and make it visible, and not much else. I think that 90% of the code that's in your main method should probably be in a class constructor. – Hovercraft Full Of Eels Mar 14 '15 at 18:01
  • The problem is that you are accessing inner class from static context. You may find possible solution here http://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method – Vladislav Lezhnin Mar 14 '15 at 18:03
  • Also, you should be using a CardLayout to swap your JPanels. Also, what is this, ` printAll(getGraphics()); //Extort print all content` supposed to be achieving? – Hovercraft Full Of Eels Mar 14 '15 at 18:04
  • Also you're using `java.awt.Frame` where you should be using `javax.swing.JFrame`. You will want to go through the Swing tutorials for more on how to create Swing GUI's. You can find links to the Swing tutorials and other Swing resources here: [Swing Info](http://stackoverflow.com/questions/tagged/swing) – Hovercraft Full Of Eels Mar 14 '15 at 18:05

0 Answers0