Could you give me an example?

What data type is the source of the event passed to the listener? Define a variable to hold that type:
<TheDataType> selectedSquare = null; //Define the variable to hold the selected square

if(selectedSquare ==  null)
   selectedSquare = ae.getSource(); // save reference to the selected square .

Would I put this outside of the Button Handler class, or inside of it?

Define the variable at the class level so it is aviable for all calls to the listener method.
The executable code goes in the listener.

What data type do you suggest?

What is the source of the event passed in the event object?
Your code compares it to what's in the squares array.

The source of the buttons is the JButton class

Did you code and test it for the three possible conditions when the listener is called: first, second same and second new?

I am going to use two variables, both of the data type JButton: one is called selectedSquare, the other called newSqaure. How would I use the variables to pint out the location of the selected sqaure and where the icon should go if the new square is then selected?

I don't know how to test the listener.

When you click on a button the listener is called with an event object. In the listener you should add code as has been discussed earlier in the thread. These last discussions were about how to save and work with a "selected" square. First click saves it, later clicks test and use it as a source or clear it if was the same square.

Could you point this out to me in my code? This is a previously modified code.

Please post your current code.

All that I have changed is adding the variables selectedSqaure and newSquare at the beginning. I have no idea what to change out of a fear of not being able to change it back. I have yet to think like a programmer on what to do next. I do as I think, not think then do

You'll have to change that to think then do. Otherwise you'll be wasting a lot of time doing things that end in a dead end.

If you have only added a variable definition you need to add the logic that uses that variable.

I think I may be in one. Do you have any suggestions? I don't have the time to scrap this code and start from the drawing board. I have already told my lab partner that if we don't figure out how to do the AI by Monday, then we would simply make it a two player game.

We have been talking about the first step in the list: selecting a square and remembering it
Most of the possibilities have been discussed several times. Make a list of the steps the code will have to do to do the selection. Get that done first. That will be the guide you'll use to write the code.

The only code to scrap is what is currently in the listener. It needs to be redesigned.

Sorry it took so long to reply. Should I scrap everything in the ButtonHandler Class, including the for loops?

I don't think the code in the listener does what you want.

Could I at least get an example of coding that could work? While the handler may not work the way I need it to work, I have seen it work in action.

Have you worked on saving and using the "selected" square yet? We talked about it a lot. My ideas are all based on that working.

I have but nothing is working. I keep thinking I have something, but it doesn't compile. I work better after seeing a good example, and I have tried looking at other types of code, but they work by clicking and dragging a piece, or the code just doesn't make sense to me. I am using an icon as the piece, and I would just like to see an example of when you click on a square, and then click on another square, the icon will move.

Post the code you have that saves the "selected" square and then does the tests we talked about earlier.

      private class ButtonHandler implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {
            for ( int i = 0; i < SIDE; i++ )
            {
               for ( int j = 0; j < SIDE; j++ )
               {
                  if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == blackPiece)
                  {
                     squares[i][j] = selectedSquare;        
                  }
                        if(selectedSquare == null && squares[i][j].getBackground() == Color.black)
                        {
                            selectedSquare = temp;
                            temp = newSquare;
                            selectedSquare.setIcon(null);
                            newSquare.setIcon(blackPiece);
                        }                   
               }
            }
         }
      }

I keep getting a null pointer exception

First get rid of the for loops. That code is not being used.

The selectedSqr should be the value of the source of the event when a button/square is clicked.
On The first click you save it, on the second click you use it as the source of the move. You detect the first click by seeing that selectedSqr is null. It won't be null for the second click.

      private class ButtonHandler implements ActionListener
      {
         public void actionPerformed( ActionEvent ae )
         {
                  if(ae.getSource() == squares[i][j] && squares[i][j].getIcon() == blackPiece)
                  {
                     squares[i][j] = selectedSquare;
                            selectedSquare = ae.getSource();        
                  }
                        if(selectedSquare == null && squares[i][j].getBackground() == Color.black)
                        {
                            selectedSquare = temp;
                            temp = newSquare;
                            selectedSquare.setIcon(null);
                            newSquare.setIcon(blackPiece);
                        }                   
         }
      }

How does this look?

Can you explain your logic? I don't understand what the code is supposed to be doing.

This is what I mean. I have spent most of my life working by example, but because I don't have a good example to go off of, I don't know what I am supposed to do. BTW, this is just the coding for the ButtonHandler class, not the whole class.

Most of the logic to handle button clicks will be in the listener.

You are going to have a hard time with programming if you can only copy from existing code. You must be able to think through what steps the program needs to take to do a task.

Without a logical description of what a method is supposed to do you can not write the code.

The second if statement was supposed to set the information in the selected square to a temporary section, which would transfer the status of the selected square to the new square, which would set the icon in the new square, and delete the icon in the old square.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.