Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: non-static variable, addActionListener()

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default non-static variable, addActionListener()

    Hi all ive been having a bita trouble adding an action listener to one of my menu items, the compiler gives me the following error . .
    non-static variable this cannot be referenced from a static context
    exitItem.addActionListener(this);
    1 error


    heres a snippet of my code that should give some detail to the program . .

    public class Project implements GLEventListener, ActionListener {
     
        static JMenuItem exitItem;
        static private Animator animator;
     
        public static void main(String[] args) {
            JFrame frame = new JFrame("Learn TCP/ IP");
            GLCanvas canvas = new GLCanvas();
     
            canvas.addGLEventListener(new Project());
            frame.add(canvas);
            frame.setSize(640, 480);
            animator = new Animator(canvas);
            frame.addWindowListener(new WindowAdapter() {
     
                @Override
                public void windowClosing(WindowEvent e) {
                    // Run this on another thread than the AWT event queue to
                    // make sure the call to Animator.stop() completes before
                    // exiting
                    new Thread(new Runnable() {
     
                        public void run() {
                            animator.stop();
                            System.exit(0);
                        }
                    }).start();
                }
            });
     
            JMenuBar titleMenu = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
     
     
            exitItem = new JMenuItem("Exit");
            exitItem.addActionListener(this); // ERROR ON THIS LINE
            fileMenu.add(exitItem);
     
            titleMenu.add(fileMenu);
     
     
     
            frame.setJMenuBar(titleMenu);
     
            // Center frame
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            animator.start();
        }

    Anyone got any ideas?? thanks in advance
    Last edited by copeg; October 31st, 2010 at 10:54 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: non-static variable, addActionListener()

    'this' doesn't refer to anything in that context. You must create an instance of your class
    exitItem.addActionListener(new Project());
    See Common Java Mistakes, a few posts down.

Similar Threads

  1. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  2. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 02:24 AM
  3. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 07:09 AM
  4. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 03:36 AM
  5. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM