Ok so basically I want this program to change the text of a random JButton out of the 9 on the panel every 5 seconds (I have a timer firing with intervals of 5 seconds) to "mole" and then wait 5 seconds, and if the user doesn't click the button in the time it should change the text back to nothing. One of the problems I'm having is that putting in Thread.sleep() to make the program wait 5 seconds messes up the whole thing and for some reason it never even changes the text if the random JButton to "mole" in the first place. The other problem I'm having is that I want it to set the text to nothing and add one to the counter variable if the button clicked has "mole" as the text, but for some reason it's not working and doesn't add to counter or change the text of the JButton to nothing(even if the requirements for the if statement have been met). Any help anyone can offer to fix either of these would be very appreciated.
import javax.swing.*; import javax.swing.Timer; import java.util.*; import java.awt.event.*; import java.awt.*; public class MoleGUI implements ActionListener{ JFrame field; JPanel panel; Timer t = new Timer(5000, this); int counter; ArrayList<JButton> holes; int randButton; public MoleGUI() { field = new JFrame("Whack-a-Mole"); panel = new JPanel(new GridLayout(3,3)); field.setContentPane(panel); field.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); holes = new ArrayList<JButton>(); //add and initialize 9 JButtons in the arrayList for(int x = 0; x < 9;x++) holes.add(new JButton()); //add the 9 JButtons to the panel for(int x = 0;x < 9;x++) panel.add(holes.get(x)); //add this as an actionListener for all 9 JButtons for(int x = 0;x < 9;x++) holes.get(x).addActionListener(this); counter = 0; } public void display(){ field.pack(); field.setVisible(true); field.resize(500,500); t.start(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == t){ randButton = (int)((Math.random() * 8) + 1); holes.get(randButton).setText("Mole"); try {Thread.sleep(1000);} catch (InterruptedException ie) { } holes.get(randButton).setText(""); } if(e.getSource() == holes.get(0) && holes.get(0).getText().equals("mole")){ counter ++; holes.get(0).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(1) && holes.get(1).getText().equals("mole")){ counter ++; holes.get(1).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(2) && holes.get(2).getText().equals("mole")){ counter ++; holes.get(2).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(3) && holes.get(3).getText().equals("mole")){ counter ++; holes.get(3).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(4) && holes.get(4).getText().equals("mole")){ counter ++; holes.get(4).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(5) && holes.get(5).getText().equals("mole")){ counter ++; holes.get(5).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(6) && holes.get(6).getText().equals("mole")){ counter ++; holes.get(6).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(7) && holes.get(7).getText().equals("mole")){ counter ++; holes.get(7).setText(""); System.out.println(counter); } if(e.getSource() == holes.get(8) && holes.get(8).getText().equals("mole")){ counter ++; holes.get(8).setText(""); System.out.println(counter); } } }