Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why are my JButtons not changing color to green? I have verified that the if statement that I am trying to get to work runs

Why are my JButtons not changing color to green? I have verified that the if statement that I am trying to get to work runs multiple time when this code is ran but it is not updating the color of the button to green. What am I doing wrong?

// call this to update player GUI after event public void updatePlayerGUI(GameBoardArray board) { playerGridPanel = new JPanel(); GridLayout layout = new GridLayout(0, 10); int squareSize = 30; playerGridPanel.setLayout(layout);

// Player Grid code int column, row; for (column = 0; column < 10; column++) { for (row = 0; row < 10; row++) {

String boardPOS = String.format("%s%s", asChar(column), row + 1); JButton square = new JButton(boardPOS); square.setPreferredSize(new Dimension(squareSize, squareSize)); square.setMaximumSize(new Dimension(squareSize, squareSize));

int thisColumn = column; int thisRow = row;

// Show ship placed square.addActionListener(e -> { int[] clickedLocation = new int[2]; clickedLocation[0] = thisColumn; clickedLocation[1] = thisRow;

PlaceShips newShip = new PlaceShips(clickedLocation); newShip.placeUserShips(board); System.out.println(thisColumn + " Column"); System.out.println(thisRow + " Row"); System.out.println(board.array[thisColumn][thisRow]);

// square.revaidate(); square.repaint(); updatePlayerGUI(board);

});

// 0 means no shots attempted at this loaction, -1 means hit, -2 means miss, // refresh board if location was a hit if (board.array[thisColumn][thisRow] == -1) { square.setBackground(Color.RED); square.setEnabled(false); }

// refresh board if location was a miss if (board.array[thisColumn][thisRow] == -2) { square.setBackground(Color.BLUE); square.setEnabled(false); }

// Show ship placed // I know this loop runs multiple times but yet no color change! WHY!? UGH!

if (board.array[thisColumn][thisRow] >= 1) {

square.setBackground(Color.GREEN); square.setOpaque(true); square.setEnabled(false); square.setBorderPainted(false); System.out.println("WHY DID I NOT TURN GREEN ");

} playerGridPanel.remove(square); playerGridPanel.add(square);

} } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

Students also viewed these Databases questions

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

State the importance of control

Answered: 1 week ago

Question

What are the functions of top management?

Answered: 1 week ago