Ok, so I am thinking this is just something dumb that I missed at some point, but I have been stuck on it for a while and can't find a solution. I have to create a game called Tsuro for a Java class I am taking. I created the board by adding a JPanel with a 6x6 grid on it to a JFrame and have stored TsuroButtons (this was a class provided by my professor for the project, it is what creates the graphics on the game's tiles). I put TsuroButtons on each tile in the grid via an array. I put an ActionListener on each tile on the board. Now, the real issue is in the ActionPerformed method at the bottom of my code. The first part is just a way for me to highlight each tile in each player's hand (A separate JFrame that is set up the same way as the board but contains the tiles that I am going to try to move to the board). The second bit is where I am having trouble.
public void actionPerformed(java.awt.event.ActionEvent e) {
TsuroButton b = (TsuroButton)e.getSource();
if(e.getActionCommand() == "Hand") {
save.setBackground(Color.white);
b.setBackground(Color.yellow);
save = b;
}
else if(e.getActionCommand() == "Board") {
b = save;
}
}
I am trying to change the value of b (the source of the event/the tile that was clicked) to the value of save, which is a field that is stored outside of the method.
TsuroButton save = new TsuroButton();
However, whenever I open up the JFrame and try to swap each tile from its spot on the player hand JFrame onto the spot that is clicked on the Board JFrame, nothing happens. I think it is something to do with JPanel, as I tried changing the background of a JPanel that I created in a test document by changing the value of the TsuroButton that was already included in the JPanel to a different TsuroButton with a different background, and although it still technically changed it there was no change in the actual JPanel being displayed.
So, in short, how do you change the value of the source in an ActionListener? Sorry that I did a terrible job of explaining this, but I really can't think of a better way to explain it than just putting all the code that is relevant to the problem on here, which isn't exactly reasonable. However, here is the constructor.
public Tsuro(int row, int column, int handsize) {
JFrame boardFrame = new JFrame();
boardFrame.setSize(1000, 1000);
boardFrame.setTitle("Tsuro");
JPanel board = new JPanel(new GridLayout(row, column));
boardFrame.add(board);
TsuroButton[][] tsuroArray = new TsuroButton[row][column];
/* This loop stores a TsuroButton at each point in the array, adds an ActionListener to them and adds them to the
* board.
* Precondition: There must be a JPanel to add the array to and an array to store the TsuroButtons and
* ActionListeners in.
* Subgoal: To store a TsuroButton and ActionListener in each part of the array and add that part to the board
* JPanel.
* */
int horizIndex = 0;
int vertIndex = 0;
int total = 0;
while(total < (row * column)) {
board.add(tsuroArray[horizIndex][vertIndex] = new TsuroButton());
tsuroArray[horizIndex][vertIndex].addActionListener(this);
tsuroArray[horizIndex][vertIndex].setActionCommand("Board");
horizIndex++;
total++;
if(horizIndex > row - 1) {
vertIndex++;
horizIndex = 0;
}
}
boardFrame.setVisible(true);
JFrame player1Hand = new JFrame();
JFrame player2Hand = new JFrame();
player1Hand.setSize(500, 200);
player2Hand.setSize(500, 200);
JPanel player1Panel = new JPanel(new GridLayout(1, handsize));
JPanel player2Panel = new JPanel(new GridLayout(1, handsize));
player1Hand.add(player1Panel);
player2Hand.add(player2Panel);
TsuroButton[] player1Array = new TsuroButton[handsize];
TsuroButton[] player2Array = new TsuroButton[handsize];
/* The purpose of this loops is to store a TsuroButton at each section in the array, set random connections on
* those TsuroButtons, to add a stone to the TsuroButtons, and to add an ActionListener.
* Preconditions: There must be an array for the player 1 and 2 hands as well as a JPanel to add each point in the
* array to.
* Subgoal: To add the completed TsuroButtons to the JPanel and repeat until finished.
* */
int index = 0;
while(index < handsize) {
player1Panel.add(player1Array[index] = new TsuroButton());
player2Panel.add(player2Array[index] = new TsuroButton());
player1Array[index].setConnections(TsuroButton.makeRandomConnectArray());
player2Array[index].setConnections(TsuroButton.makeRandomConnectArray());
player1Array[index].addStone(Color.BLUE, 6);
player2Array[index].addStone(Color.GREEN, 2);
player1Array[index].addActionListener(this);
player2Array[index].addActionListener(this);
player1Array[index].setActionCommand("Hand");
player2Array[index].setActionCommand("Hand");
index++;
}
player1Hand.setVisible(true);
player2Hand.setVisible(true);
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {
}
}