1

I am currently coding a game and part of it consist of having different tiles to be put in a board. I plan on simulating this by having different buttons that will be used to represent the tiles with their corresponding coordinates. For example, one button will say "A1", "A2", etc. What I would like to accomplish, is to have the user click on the "A1" tile and then the button on the board that represents "A1" will change colors, is there any way to go through the buttons on the board and compare its text to the selection of the user? The following is what I used to create the board:

    JButton[][] buttons = new JButton[9][12];
    JPanel panel = new JPanel(new GridLayout(9,12,5,5));
    panel.setBounds(10, 11, 800, 600);
    frame.getContentPane().add(panel);


    //board
     for (int r = 0; r < 9; r++)
        {
            for (int c = 0; c < 12; c++)
            {
                buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
                buttons[r][c].setBackground(Color.WHITE);
                panel.add(buttons[r][c]);
            }

        }

This is what I wrote on the code of one of the tiles

JButton tile1 = new JButton ("A1");
        tile1.setBounds(60,725,60,60);
        frame.getContentPane().add(tile1);

        tile1.addActionListener(new ActionListener() 
         { 
                     public void actionPerformed(ActionEvent e) 
            { 
                String buttonText = tile1.getText();

                // iterating through all buttons:

                for(int i=0;i<buttons.length;i++){
                    for(int j=0;j<buttons[0].length;j++)
                    {
                        JButton b = buttons[i][j];
                        String bText = b.getText(); 

                       if(buttonText.equals(bText))
                        {
                            [i][j].setBackground(Color.BLACK);
                        }
                    }
                }
             } 
          } );

However, it is given me an error saying that there is an action expected after "{"

ZeldaX
  • 49
  • 9

2 Answers2

2

You may add an action listener to each of the JButton you are creating in the loop like below:

buttons[r][c].addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent e) { 
    // your code here
  } 
} );

Placing the listener in your code may look like

JButton[][] buttons = new JButton[9][12];
    JPanel panel = new JPanel(new GridLayout(9,12,5,5));
    panel.setBounds(10, 11, 800, 600);
    frame.getContentPane().add(panel);


    //board
     for (int r = 0; r < 9; r++)
        {
            for (int c = 0; c < 12; c++)
            {
                buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
                buttons[r][c].setBackground(Color.WHITE);
                buttons[r][c].addActionListener(new ActionListener() { 
                   public void actionPerformed(ActionEvent e) { 
                      JButton button = (JButton) e.getSource();
                      String buttonText = button.getText();
                      // now iterate over all the jbuttons you have
                      for(int i=0;i<buttons.length;i++){
                          for(int j=0;j<buttons[0].length;j++){
                              JButton b = buttons[i][j];
                              String bText = b.getText();
                              if(buttonText.equals(bText)){
                                  // you found a match here
                                  // and you have the positions i, j
                                  // 
                              }
                          }
                      }
                   } 
                } );
                panel.add(buttons[r][c]);
            }

        }

And you could store the the colors to be changed to in global static array, and use that array in your action listener.

For information on adding listener to the JButton, you may refer this thread How do you add an ActionListener onto a JButton in Java

Hope this helps!

arjun
  • 1,645
  • 1
  • 19
  • 19
  • than you so much for your answer! however, I still dont understand how to compare the text of the two different JButtons – ZeldaX May 24 '17 at 05:00
  • I was thinking of having a method that calls itself when a tile is clicked and then it will go through the board tiles, find the one that corresponds and then change the color @Mallikarjun M – ZeldaX May 24 '17 at 05:07
  • I have edited actionPerformed method. Pleae check if it is ok for your case. Also, I haven't compiled it, as there is no full code. There could be some errors, but I think you may get the idea.. – arjun May 24 '17 at 05:34
  • Thank you so much! Just to make sure, does the actionPerformed method actives when any button is clicked? Because the user wont be clicking the tiles on the Panel Board. Instead, I will place buttons on the bottom of the screen representing tiles - and the text will have the coordinates available to the user- that when clicked will change the color of the corresponding buttons on the board – ZeldaX May 24 '17 at 05:58
  • @ZeldaX Yes, the actionPerformed method actives when any button is clicked, as we are setting the action listener for all the jbuttons. – arjun May 24 '17 at 06:13
  • I just tried it and its giving me an error. I added the error on the question. Also, I removed the iteration through the JButtons on the board and added the same actionListener to each of the tiles that the user will be pressing, is that correct? – ZeldaX May 24 '17 at 14:39
1

You need listeners.

Implement ActionListener to your class. This will require you to add public void actionPerformed(ActionEvent e) {} to your class.

Every JButton you use should have an action listener. Apply one like that:

JButton but = new JButton();
but.addActionListener(this);

Finally, in the actionPerformed method we added, you need to add something like that:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == but)
        but.setBackground(Color.BLACK);
}

P.S. you can get a button's text value by the means of:

but.getText();
nic3ts
  • 347
  • 1
  • 2
  • 13
  • thank your for your response! I added the actionListener on the loop so each tile on the board will have one. However, I still don't understand how to make it compare the text on the separate tiles with the ones on the board. I was thinking of having a method that calls itself when a tile is clicked and then it will go through the board tiles, find the one that corresponds and then change the color. – ZeldaX May 24 '17 at 05:05
  • You don't need to find the one that corresponds because the listener does that exact job for you. It "listens". Once the user interacts with the listener something will happen (i specified it in the actionPerformed method. The only problem should be that but (the button i used as an example) must be declared on a global scope. – nic3ts May 24 '17 at 05:15
  • Which means if the user presses button with "A1", the A1 button will turn black. If you wish to get the text value from the button, i wrote you how to do so bellow my P.S. – nic3ts May 24 '17 at 05:17
  • I am sorry, I am still a little confused. Youre saying that I need to add the actionListener to both, the tiles and JButtons on the board, correct? and then whatever I write on the method will occur after the user clicks on the tile?. You wrote "but.setBackground" but I won't know which tile will be clicked and therefore dont know which button on the board will have to change colors. What I understand is that "e" is invoked whenever any button is clicked and then .getSource gets the text and compares it to the button? – ZeldaX May 24 '17 at 05:35
  • First of, you don't have both tiles and jbuttons. With what you're creating, you'll have a JPanel with a GridLayout consisting of Jbuttons. Each button gets a listener, so if a listener is activated, it's activated only for the button you clicked, therefor whatever code is written in the listener of A1's button, will only be triggered when A1 is pressed. – nic3ts May 24 '17 at 05:43
  • @ZeldaX .getSource gets the button to you. And you can change the color of it. – arjun May 24 '17 at 05:43
  • I will also have tiles that the user clicks, I just haven't coded them yet that is why I didnt add them on the code. The idea is that those tiles will be buttons that when clicked will change the color of the Buttons on the BoardPanel – ZeldaX May 24 '17 at 05:53
  • I see. In that case, the "tiles", i'm guessing will also be JButtons, right? – nic3ts May 24 '17 at 05:56
  • In my opinion the "tiles" shouldn't be used, the user can click on the button on the board directly for it to change color. – nic3ts May 24 '17 at 05:57
  • Either way, if your tiles are JButton, then check that the .getText() of the button you're pressing, is same with the .getText() of one of the JButtons in buttons[r][c] array. If this happens, there's your match and you can change anything you want in the if scope. – nic3ts May 24 '17 at 05:59
  • I need the tiles because the user will be assigned 7 random coordinates on each turn, and yes they would be JButtons – ZeldaX May 24 '17 at 06:00
  • No, only the tiles will have listeners, the jbuttons on your board don't need :) What you need is "if tile is pressed (listener required to grasp that) then change color of this button" – nic3ts May 24 '17 at 06:05
  • I tried adding the action listener to each of the tiles but its giving me an error. I added the code on the question! what am I doing wrong? – ZeldaX May 24 '17 at 14:41