Results 1 to 4 of 4
  1. #1
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default ButtonWatcher implements ActionListener

    Hi I'm trying to use method from other class within ButtonWatcher class but notsure how to do that (both classes are in the came package and method id public).

    the code that Im working on is:

    Java Code:
        private class ButtonWatcher implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                Object buttonPressed = a.getSource();
                if (buttonPressed.equals(playButton))
                {
                    gameUnderWay = true;
                }
                if (buttonPressed.equals(xButton))
                {
                    TicTacToe.setFirstPlayerSymbol(0);
                    startingGameButtons();
                }
                if (buttonPressed.equals(oButton))
                {
                    setFirstPlayerSymbol(a);
                    startingGameButtons();
                }
            }
    and the method that Im trying to implement is in TicTacToe.java class:

    Java Code:
        public void setFirstPlayerSymbol(int chosenSymbol) // given
        {
            players[0] = chosenSymbol;
            players[1] = - chosenSymbol;
        }
    I know it must be silly mistake but I really need advide how to implement that method.

  2. #2
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    i.e. this comes up with error: non-static method cannot be referenced from static context:

    Java Code:
        /**
         *It lisen for clicks of any of the three buttons:
         *xButton, oButton and playButton,
         */    
        private class ButtonWatcher implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                Object buttonPressed = a.getSource();
                if (buttonPressed.equals(playButton))
                {
                    gameUnderWay = true;
                    
                }
                if (buttonPressed.equals(xButton))
                {
                    playInstructions();
                    startingGameButtons();
                    
    
                    TicTacToe.setFirstPlayerSymbol(1);
                }
                if (buttonPressed.equals(oButton))
                {
                    playInstructions();
                    startingGameButtons();
                    setFirstPlayerSymbol(-1);
                }
            }
        }
    Please please... How to fix that?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by darkblue24 View Post
    i.e. this comes up with error: non-static method cannot be referenced from static context:

    Please please... How to fix that?
    I'm sure the compiler also gave you the line number and the offending statement or expression. Why do you make us guess? When you want us to look at your code be as explicit as possible.

    kind regards,

    Jos

  4. #4
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

Similar Threads

  1. How hibernate implements jpa specification?
    By king9527 in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2010, 11:53 AM
  2. Hashtable implements ActionListener ?
    By Zayaan in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 08:49 PM
  3. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 06:19 AM
  4. HashSet and AbstractSet both Implements Set
    By rkjsohi2 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 12:04 PM
  5. How to implements cryptation in a java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •