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 7 of 7

Thread: Need help with ActionListener Error

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Need help with ActionListener Error

    Here is my code. It's for a tic tac toe program and i'm trying to get my Listener to work so it knows what to display but it's throwing me an error.

    Code first here it is:

     
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
     
     
    public class TicTacToeGame3
    {
        public static void main(String[] args)
        {  
        final int turn = 1;
        String Oplayer = "O";
        String Xplayer = "X";
        final JButton[] buttons;
     
          JFrame frame = new JFrame();
          final JLabel label = new JLabel("Click New Game to Begin");
          frame.add(label, BorderLayout.NORTH);
     
          //JPanel orderPanel = new JPanel();
          final JButton button = new JButton("New Game");
          JPanel southPanel = new JPanel();
          southPanel.add(button);
          frame.add(southPanel, BorderLayout.SOUTH);
     
          buttons = new JButton[9];
          for (int i=0; i<9; i++)
          {
     
    			buttons[i]=new JButton("");
          }
     
          JPanel buttonsPanel = new JPanel();
     
            buttonsPanel.setLayout(new GridLayout(3, 3));
          for(int i = 0; i < 9; i++) 
           {
                buttonsPanel.add(buttons[i]);
            }
     
          frame.add(buttonsPanel, BorderLayout.CENTER);
     
          //Button Listener
     
         class ResetGame implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                if (button.isSelected());
                label.setText("X Starts The Game");
     
     
     
               // buttons1.setText("");
               // buttons1.setEnabled(true);
     
             }
          }
     
          ResetGame reset = new ResetGame();
          button.addActionListener(reset);
     
          class ButtonListenerCenter implements ActionListener
             {
              public void actionPerformed(ActionEvent event)
                {
                  /* if (buttons1.isSelected());
                   buttons1.setText("X");
                   buttons1.setEnabled(false);
     
     
                   if (buttons2.isSelected());
                   buttons2.setText("O");
                   buttons2.setEnabled(false);*/
     
     
     
     
                   //even and odd for x's and o's 
                   //Has to be some sort of user1 and user2 click events
                }
             }
          //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter();
          //buttons.addActionListener(buttonListener2);
     
     
     
    class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent event) 
         {
             Object source = event.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
               // buttonsClicked = true;
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
                   //pnlPlayingField.requestFocus();
                   //turn++;
     
          }
        }
     
          ActionListener clickListener = new ClickListener();
          buttons.addActionListener(clickListener);
          }
          }
     
     
     
     
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setVisible(true);
     
       }
     
    }

    Here is my error that I'm getting I just don't know how to fix it and need to be pointed in the right direction

    TicTacToeGame3.java:108: error: cannot find symbol
    buttons.addActionListener(clickListener); This is the part I'm having trouble with if this is commented out the program will run.
    ^
    symbol: method addActionListener(ActionListener)
    location: variable buttons of type JButton[]
    1 error


    Working code that will compile if that part of code is commented out.
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
     
     
    public class TicTacToeGame3
    {
        public static void main(String[] args)
        {  
        final int turn = 1;
        String Oplayer = "O";
        String Xplayer = "X";
        final JButton[] buttons;
     
          JFrame frame = new JFrame();
          final JLabel label = new JLabel("Click New Game to Begin");
          frame.add(label, BorderLayout.NORTH);
     
          //JPanel orderPanel = new JPanel();
          final JButton button = new JButton("New Game");
          JPanel southPanel = new JPanel();
          southPanel.add(button);
          frame.add(southPanel, BorderLayout.SOUTH);
     
          buttons = new JButton[9];
          for (int i=0; i<9; i++)
          {
     
    			buttons[i]=new JButton("");
          }
     
          JPanel buttonsPanel = new JPanel();
     
            buttonsPanel.setLayout(new GridLayout(3, 3));
          for(int i = 0; i < 9; i++) 
           {
                buttonsPanel.add(buttons[i]);
            }
     
          frame.add(buttonsPanel, BorderLayout.CENTER);
     
          //Button Listener
     
         class ResetGame implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                if (button.isSelected());
                label.setText("X Starts The Game");
     
     
     
               // buttons1.setText("");
               // buttons1.setEnabled(true);
     
             }
          }
     
          ResetGame reset = new ResetGame();
          button.addActionListener(reset);
     
          class ButtonListenerCenter implements ActionListener
             {
              public void actionPerformed(ActionEvent event)
                {
                  /* if (buttons1.isSelected());
                   buttons1.setText("X");
                   buttons1.setEnabled(false);
     
     
                   if (buttons2.isSelected());
                   buttons2.setText("O");
                   buttons2.setEnabled(false);*/
     
     
     
     
                   //even and odd for x's and o's 
                   //Has to be some sort of user1 and user2 click events
                }
             }
          //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter();
          //buttons.addActionListener(buttonListener2);
     
     
     
    class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent event) 
         {
             Object source = event.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
               // buttonsClicked = true;
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
                   //pnlPlayingField.requestFocus();
                   //turn++;
     
          }
        }
     
          ActionListener clickListener = new ClickListener();
          //buttons.addActionListener(clickListener);
          }
          }
     
     
     
     
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setVisible(true);
     
       }
     
    }


    Any help is very much appreciated because I'm just not understanding and I should or need to. There is a lot of code that won't be there I'm just troubleshooting the ClickListener part at the moment. Thanks in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Need help with ActionListener Error

    Looks like you're trying to add an ActionListener to an array. You can't do that. Instead, loop through each JButton in the array and add the ActionListener to them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ActionListener Error

    Thanks for the quick response, I thought that would be the case just wasn't sure. So loop through each JButton in the array and add the ActionListener to them. This is what I did and I'm getting compile errors. Here is my code now.

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
     
     
    public class TicTacToeGame3 
    {
        public static void main(String[] args)
        {  
        final int turn = 1;
     
        final JButton[] buttons;
     
          JFrame frame = new JFrame();
          final JLabel label = new JLabel("Click New Game to Begin");
          frame.add(label, BorderLayout.NORTH);
     
          //JPanel orderPanel = new JPanel();
          final JButton button = new JButton("New Game");
          JPanel southPanel = new JPanel();
          southPanel.add(button);
          frame.add(southPanel, BorderLayout.SOUTH);
     
          buttons = new JButton[9];
          ActionListener clickListener = new ClickListener();
          for (int i=0; i<9; i++)
          {
     
    			buttons[i]=new JButton("");
             buttons[i].addActionListener(clickListener);
          }
     
          JPanel buttonsPanel = new JPanel();
     
            buttonsPanel.setLayout(new GridLayout(3, 3));
          for(int i = 0; i < 9; i++) 
           {
                buttonsPanel.add(buttons[i]);
            }
     
          frame.add(buttonsPanel, BorderLayout.CENTER);
     
     
     
         class ResetGame implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                if (button.isSelected());
                label.setText("X Starts The Game");
     
     
     
               // buttons1.setText("");
               // buttons1.setEnabled(true);
     
             }
          }
     
          ResetGame reset = new ResetGame();
          button.addActionListener(reset);
     
          class ButtonListenerCenter implements ActionListener
             {
              public void actionPerformed(ActionEvent event)
                {
                  /* if (buttons1.isSelected());
                   buttons1.setText("X");
                   buttons1.setEnabled(false);
     
     
                   if (buttons2.isSelected());
                   buttons2.setText("O");
                   buttons2.setEnabled(false);*/
     
     
     
     
                   //even and odd for x's and o's 
                   //Has to be some sort of user1 and user2 click events
                }
             }
          //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter();
          //buttons.addActionListener(buttonListener2);
     
     
     
    class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent event) 
         {
             Object source = event.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
               // buttonsClicked = true;
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
                   //pnlPlayingField.requestFocus();
                   //turn++;
     
          }
        }
     
     
          //buttons.addActionListener(clickListener);
         }
         }
     
     
     
     
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setVisible(true);
     
       }
     
    }

    The new error is for the ClickListener Class here it is

    TicTacToeGame3.java:26: error: cannot find symbol
    ActionListener clickListener = new ClickListener();
    ^
    symbol: class ClickListener
    location: class TicTacToeGame3
    1 error

    This is what I switch in my code

     buttons = new JButton[9];
          ActionListener clickListener = new ClickListener();
          for (int i=0; i<9; i++)
          {
     
    			buttons[i]=new JButton("");
             buttons[i].addActionListener(clickListener);
          }

    Is this how to loop through each JButton in the array or am I totally lost and not understanding.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Need help with ActionListener Error

    Where do you have your ClickListener class defined?

    It looks like you're trying to use it in your main method, so ClickListener must either be in its own file or be a static inner class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ActionListener Error

    It's supposed to be an inner class within the main method here is the code for it

    class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent event) 
         {
             Object source = event.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
     
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
     
                   //turn++;
     
          }
        }


    --- Update ---

    Ok I put it into a new class and can get it to display a system.out.println. I guess now my question is how can I get my arguments into this new file for example how do I get this code into the new Click Listener file without errors I'm having troubles with this now and it shouldn't be to hard I thought it was just using
    TicTacToeGame3 clickListener = new TicTacToeGame3 in the new files main method but I'm way off I think

     class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent event) 
         {
             Object source = event.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
     
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
     
                   //turn++;
     
          }
        }


    Here are the two files with no compile errors

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
     
     
    public class TicTacToeGame3
    {
        public static void main(String[] args)
        {  
        final int turn = 1;
        final JButton[] buttons;
     
          JFrame frame = new JFrame();
          final JLabel label = new JLabel("Click New Game to Begin");
          frame.add(label, BorderLayout.NORTH);
     
          //JPanel orderPanel = new JPanel();
          final JButton button = new JButton("New Game");
          JPanel southPanel = new JPanel();
          southPanel.add(button);
          frame.add(southPanel, BorderLayout.SOUTH);
     
          buttons = new JButton[9];
          ActionListener clickListener = new ClickListener();
          for (int i=0; i<9; i++)
          {
     
    			buttons[i]=new JButton("");
             buttons[i].addActionListener(clickListener);
          }
     
          JPanel buttonsPanel = new JPanel();
     
            buttonsPanel.setLayout(new GridLayout(3, 3));
          for(int i = 0; i < 9; i++) 
           {
                buttonsPanel.add(buttons[i]);
            }
     
          frame.add(buttonsPanel, BorderLayout.CENTER);
     
          //Button Listener
     
         class ResetGame implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                if (button.isSelected());
                label.setText("X Starts The Game");
     
     
     
               // buttons1.setText("");
               // buttons1.setEnabled(true);
     
             }
          }
     
          ResetGame reset = new ResetGame();
          button.addActionListener(reset);
     
          class ButtonListenerCenter implements ActionListener
             {
              public void actionPerformed(ActionEvent event)
                {
                  /* if (buttons1.isSelected());
                   buttons1.setText("X");
                   buttons1.setEnabled(false);
     
     
                   if (buttons2.isSelected());
                   buttons2.setText("O");
                   buttons2.setEnabled(false);*/
     
     
     
     
                   //even and odd for x's and o's 
                   //Has to be some sort of user1 and user2 click events
                }
             }
          //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter();
          //buttons.addActionListener(buttonListener2);
     
     
     
    class ClickListener implements ActionListener
       {     
         public void actionPerformed(ActionEvent click) 
         {
             Object source = click.getSource();
             for(int i=1; i<=9; i++) {
                if(source == buttons[i] && turn < 10) {
               // buttonsClicked = true;
                if(!(turn % 2 == 0))
                buttons[i].setText("X");
                   else
                   buttons[i].setText("O");
                   buttons[i].setEnabled(false);
                   //pnlPlayingField.requestFocus();
                   //turn++;
     
          }
        }
     
     
          //buttons.addActionListener(clickListener);
         }
         }
     
     
     
     
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setVisible(true);
     
       }
     
    }


    The new file for the ClickListener Class
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class ClickListener implements ActionListener
    {
       public void actionPerformed(ActionEvent event)
       {
          System.out.println("Yeah, I was clicked!");
       }
    }

    Now when I click any button in my GUI on the center panel a message displays saying yeah I was clicked. What I want to do now is get X and O to displays based on odd and even users which I want to be the argument for the main method of the ClickListener file but I'm having trouble getting it to find the variables and arrays set in the TicTacToeGame3 file. Thanks again for working through this with me as I've only been programming for 2 months now.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Need help with ActionListener Error

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ActionListener Error

    Yeah I've read about 11 chapters in my Big Java book and Chapter 10 was all about this. I just don't understand what is going wrong when applying this class to the array and why it won't find that class if it's in my program as an inner class. I have no trouble with the new game button and using the ResetGame class as an inner class to get the ActionListener to work. For some reason it's just working with arrays that really messes stuff up for me.

    I don't know, I will look into it more and see if I can figure it out with just the one file because I don't want to use a separate file, I just want inner classes with one file.

Similar Threads

  1. Need help with ActionListener if-else
    By texasPI in forum Loops & Control Statements
    Replies: 4
    Last Post: September 6th, 2013, 07:18 PM
  2. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  3. Error when adding ActionListener to JButton
    By grimrader22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 05:53 AM
  4. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2011, 11:45 PM
  5. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM