1

As part of an exercise, I would like to design a program that displays a 9x9 grid of JButtons. When I click on a button, I would like that button to change in some way, like say originally it displays an 'o', but when you click on it, it displays an 'x', or maybe it changes colors, etc., while the other buttons remain unchanged.

However, I am not sure how to do this. I have created a 2D array of JButtons, and placed each on in a 9x9 GridLayout panel. I also set an ActionListener for each one. The problem is, I do not know how to change the color or text of only one button. Here is the short version of my program only displaying the relevant parts.

private JButton[][] t = new JButton[9][9]; //Declared much earlier in the program, right after the class declaration.

public void buildTile()
{
    panelc.setLayout(new GridLayout(9, 9));
    for(int r = 0; r < 9; r++)
    {
        for(int c = 0; c < 9; c++)
        {
            t[r][c] = new JButton("O");
            t[r][c].setBackground(Color.BLACK);
            t[r][c].setForeground(Color.WHITE);
            t[r][c].addActionListener(new TileListener());
            panelc.add(t[r][c]);
        }
    }
}

private class TileListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        //Some code to change a specific button
    }
}

How may I specify which button to perform some sort of aesthetic change to?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Yui Horie
  • 13
  • 1
  • 3

1 Answers1

-2

In the actionlistener e.getSource() will return the button you clicked

  • Yes, but printing to a console only returns something like: javax.swing.JButton[,2,2,66x49,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1b3c169,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=O,defaultCapable=true] There's nothing else for me to do with e.getSource() that would be helpful. – Yui Horie Apr 21 '16 at 20:27
  • Okay, to make it clear: You get an Object by calling e.getSource(). You should cast it to JButton: ((JButton) e.getSource()) Then you can do whatever you want with it, f.e.: ((JButton) e.getSource()).setText("...") – Attila Horváth Apr 21 '16 at 20:30
  • That works perfectly. I apologize, but I am fairly new to Java, and have not seen this technique applied before. Could you possibly explain what exactly this statement does? – Yui Horie Apr 21 '16 at 20:39
  • Sure! e is the event (mouse click now) getSource() method gets the source of the event, so it will return the button you clicked on. But this method is not from JButton, so it will return an Object, but Object does not have setText() method, so you have to cast it to JButton. (try casting to something else like JLabel, you will get ClassCastException because it is not a JLabel) ((JButton) e.getSource) does this, and now you have the JButton you clicked, and do whatever you want with it. If you want to make more changes, use: JButton clickedButton = (JButton) e.getSource(); ... – Attila Horváth Apr 21 '16 at 20:45
  • Thank you. I can't believe I had never used casting before. This would have saved me a lot of trouble in previous exercises. – Yui Horie Apr 21 '16 at 20:50