Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to loop through an array of buttons, and change their colors 3 times as iterate through the array. When i click the first button of the array, it changes the colors of the rest, as i want it to. When i go and try it again, it does not run the try block in my run() method.

Here is the code.

Java
public class ButtonWindow extends JFrame implements Runnable {
	private JPanel topPanel = new JPanel(new GridBagLayout());
	JButton[] listButtons = new JButton[5];
	private Thread t;
	
	public void run() {
	      System.out.println("Running thread");
	      try {
	         for(int i = 1; i < listButtons.length; i++) {
	            System.out.println("Thread changing colors");
	      
					listButtons[i].setBackground(Color.YELLOW);
					Thread.sleep(1000);
					listButtons[i].setBackground(Color.ORANGE);
					Thread.sleep(1000);
					listButtons[i].setBackground(Color.RED);
					Thread.sleep(1000);
					listButtons[i].setBackground(null);
					
	         }
	     } catch (InterruptedException e) {
	         System.out.println("Thread interrupted.");
	     }
	     System.out.println("Thread closing.");
	   }//close run() method
	
	public void start(){
		System.out.println("Starting thread ");
		if(t == null){
			t = new Thread(this);
			t.start();
		}
	}//close start() method
	
	//CONSTRUCTOR
	public ButtonWindow(){
		//create the buttons
		for(int i = 0; i < 5; i++){
			 listButtons[i] = new JButton(String.valueOf(i));
		 }
		setSize(400,400);
		setTitle("Colors");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		buildTopPanel();
		//BUTTON LISTENERS/////////////////////////////////////////////////////////////////////////////////		
		listButtons[0].addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				 System.out.println("Creating thread");
			      start();
			}
		});
		//BUTTON LISTENERS END///////////////////////////////////////////////////////////////////	
	}//CLOSE CONSTRUCTOR

	//CREATE PANEL
	public void buildTopPanel(){
		
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		for(int i = 0; i < 5; i++){
			 //listButtons[i] = new JButton(String.valueOf(i));
			 gc.insets = new Insets(5, 10,0,0);
			 topPanel.add(listButtons[i],gc);
			 gc.gridy ++;
		 }
		add(topPanel,BorderLayout.NORTH);
	}  
}//CLOSE CLASSS
Posted
Updated 26-Feb-15 20:56pm
v2

1 solution

That's because t is not null the second time you run your start method, so the thread never get's started again.

Try setting t to null at the end of the run method, that will allow the color-cycling to restart.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
jchacon28 27-Feb-15 8:37am    
Works every time now, thanks!
Fredrik Bornander 27-Feb-15 10:32am    
Cool. Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900