1,105,423 Community Members

Panel Background Based On Timer

Member Avatar
guru_iyer
Light Poster
34 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to change the background color of the JPanel every 3 seconds (3000 ms) when I click START button till I press STOP button.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

class TimerBackground implements ActionListener
{
	JFrame frame; 	JPanel panel;
	JButton btnStart; 	JButton btnRed;
	JButton btnGreen; 	JButton btnBlue;
	JButton btnStop; 	Timer t;  
	Random num = new Random();  
	int r, g, b;
	TimerBackground()
	{
		r = g = b = 0;
		frame = new JFrame("Timer Background");
		panel = new JPanel();
		btnStart = new JButton("START");
		btnRed = new JButton("Red");
		btnGreen = new JButton("Green");
		btnBlue = new JButton("Blue");
		btnStop = new JButton("STOP");
		t = new Timer(3000, this);

		btnStart.addActionListener(this);	
		btnStop.addActionListener(this);
		btnRed.addActionListener(this);
		btnBlue.addActionListener(this);
		btnGreen.addActionListener(this);
		t.addActionListener(this);
	
		frame.setSize(800,600);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setContentPane(panel);
		panel.add(btnStart);
		panel.add(btnStop);
		panel.add(btnRed);
		panel.add(btnGreen);

		t.start();
	}
	
	public void actionPerformed(ActionEvent evnt)
	{
		if(evnt.getSource() == btnRed)
		{
			panel.setBackground(new Color(255,0,0));
		}
		else if(evnt.getSource() == btnGreen)
		{
			panel.setBackground(new Color(0,255,0));
		}
		else if(evnt.getSource() == btnBlue)
		{
			panel.setBackground(new Color(0,0,255));
		}
		else if(evnt.getSource() == btnStart)
		{
			//Are The Lines Below Correct or else What Should I Write Here??
			r = num.nextInt(255);
			g = num.nextInt(255);
			b = num.nextInt(255);
			panel.setBackground(new Color(r, g, b));
		}
		else if(evnt.getSource() == btnStop)
		{
			t.stop();
		}
	}

	public static void main(String[] args)
	{
		TimerBackground timerB = new TimerBackground();
	}
}

The above code works only once on click of START button. I don't understand how to make it run every 3 seconds.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

javax.swing.Timer is what you need - documentation & samples in the usual places.

Define a timer task (a "run()" method) that you want to run repeatedly. On click of the start button start a Timer that will run that task every 3000 msec. On click of the stop button stop the timer.

Member Avatar
guru_iyer
Light Poster
34 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

But I don't know how to implement it, can u give me a sample?

Member Avatar
guru_iyer
Light Poster
34 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

PLease let me know what modifications can i make to my code??

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

READ THE DOCUMENTATION!

http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html
http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html

If you can't use Google to find code samples, tutorials, and reference materials you will never be able to write programs, in any language. Getting someone else to do it for you is not an answer.

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

@ JamesCherrill

hard of gold :-)

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Hey! Was that deliberate? Normal phrase is "heart of gold", but you wrote "hard" as in tough. difficult etc. Neat pun!
I still stand by what I said.

Member Avatar
cretaros
Junior Poster in Training
70 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

also implement a loop if that operation is to keep on being repeated

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

thank you, my fast hands and still problems wrote English words correctly, nor ...

but my reaction == this forum is long times one man show throught EU working hours, with my respect man

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

also implement a loop if that operation is to keep on being repeated

No no no.

The javax.swing.Timer handles the repeating. If you tell a newbie to use a loop in this context you will get a loop with a sleep in it that blocks the EDT and leads inevitably to the post that goes "why isn't by background updating?".

So, no, no loop. Just a swing Timer.

Member Avatar
guru_iyer
Light Poster
34 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey I used TimerTask one as suggested by James Cherrill... Thanks.. I had to dig deep, but I found out..

Question Answered as of 3 Years Ago by JamesCherrill, mKorbel and cretaros
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: