First you will need a buttonhandler for your JCheckbox and then you add your new JButton to the button handler then you will add a actionListener, ill give you an example i have for a GUI that has a JButton, essential the same thing. then in your ButtonHandler make it change all the values you want code with an example for this will also be below
public class GameBoard extends JFrame{
private static final Color red = new Color(200,55,55);
private static final Color blue = new Color(55,55,200);
private static final Color green = new Color(55,220,55);
private static final int SIZE = 10;
static JButton[][] tiles;
private static boolean cpTurn = false;
public static void main(String[] args){
tiles = new JButton[SIZE][SIZE];
JLabel label = new JLabel();
GameBoard gb = new GameBoard();
JButton refreashButton = new JButton("Refreash");
ButtonHandler listener = new ButtonHandler();
refreashButton.addActionListener(listener);
refreashButton.setName("refreash");
JPanel mineField = new JPanel();
mineField.setLayout(new GridLayout(10,10));
mineField.setSize(500,500);
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
tiles[i][j] = new JButton();
tiles[i][j].setSize(50,50);
tiles[i][j].setVisible(true);
tiles[i][j].setBorderPainted(false);
tiles[i][j].setContentAreaFilled(false);
tiles[i][j].setOpaque(true);
tiles[i][j].setBackground(green);//green
tiles[i][j].addActionListener(listener);
tiles[i][j].setName(i+""+j);
mineField.add(tiles[i][j]);
}
}
JPanel board = new JPanel();
board.setLayout(new GridLayout(2,1));
board.add(mineField);
board.add(refreashButton);
board.setBackground(green);*/
JPanel board = new JPanel();
board.setLayout(new BoxLayout(board, BoxLayout.Y_AXIS));
board.add(mineField);
board.add(refreashButton);
JFrame window = new JFrame("BoardGame");
window.setContentPane(board);
window.setSize(600,700);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton btn = (JButton)e.getSource();
if(cpTurn == false){
if(btn.getName().equals("refreash")){
cpTurn = false;
}
else{
cpTurn = true;
}
}
else {
cpTurn = false;
}
if(btn.getName().equals("refreash")){
for(int i = 0; i < SIZE; i ++){
for(int j = 0; j < SIZE; j++){
tiles[i][j].setBackground(green);
}
}
}
else if(btn.getBackground().equals(green)){
btn.setBackground(red);
}
else if(btn.getBackground().equals(red)){
btn.setBackground(blue);
String name = btn.getName();
int x = Integer.parseInt(name.substring(0,1));
int y = Integer.parseInt(name.substring(1));
if(x - 1 >= 0 && tiles[x-1][y].getBackground().equals(red) ){
tiles[x-1][y].setBackground(blue);
}
if(x + 1 <= 9 && tiles[x+1][y].getBackground().equals(red) ){
tiles[x+1][y].setBackground(blue);
}
if(y + 1 <= 9 && tiles[x][y+1].getBackground().equals(red)){
tiles[x][y+1].setBackground(blue);
}
if(y - 1 >= 0 && tiles[x][y-1].getBackground().equals(red)){
tiles[x][y-1].setBackground(blue);
}
}
else if(btn.getBackground().equals(blue)){
btn.setBackground(green);
}
if(cpTurn){
int randx = (int)(Math.random() *(10));
int randy = (int)(Math.random() *(10));
tiles[randx][randy].doClick();
}
}
}
}