setBackground Color problem
Dear all,
New to Java (green and keen). I have a form with two buttons on it. I want it to change the background of the button from its default color to red, wait a second, then change to blue.
I am using the following code:
public void actionPerformed(ActionEvent arg0) {
btnColor.setBackground(Color.RED);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btnColor.setBackground(Color.PINK);
All that happens is it pauses for a second then changes to pink. It never goes red.
What am I doing wrong?
Kev
Re: setBackground Color problem
You need to give the JVM's thread back to it so it can change the color. The actionPerformed method is using the JVMs thread that it uses for GUI management. You need to change the color, release the thread by exiting the method and let the JVM use its thread to change the color. Then at a later time you can change the color again and give control back to the GUI thread to allow it to change the color.
You could use a Timer to wait a short time and to call a method that will make the second color change.
Your code and the JVM's code take turns using the thread.
Re: setBackground Color problem
Thanks for that,
Can you point me in the right direction to solve this please?
Thanks,
kevin
Re: setBackground Color problem
Read up on: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing). You can use the SwingWorker as the background Thread. You can "publish" the background color, then sleep() and then publish the second color.
Re: setBackground Color problem
Thanks for that, still a bit confused, but I will work it out.
Kev
Re: setBackground Color problem
Nope, my brain is beginning to hurt. Could someone provide a clue /code example to help me here.
Thanks in advance,
Kevin
Re: setBackground Color problem
In the actionPerformed method set the first color and start a Timer to call another actionPerformed method in 1 second and exit the actionPerformed method. When the Timer calls the other actionPerformed method set the color to the second color and exit the method.
Do a Search here on the forum for code samples using the Timer class
Re: setBackground Color problem
The tutorial has a working example. It shows you how to "flip a coin" and display the results each time a flip is done.
Your code will be easier because all you do is publish a Color, sleep() and then publish the second color. No need for a loop.
Re: setBackground Color problem
if u wish try the following code
import java.awt.*;
import java.awt.event.*;
public class colordemo extends Frame implements ActionListener
{
Button b1,b2,b3;
boolean a= false;
colordemo(String s)
{
super(s);
setLayout(new FlowLayout());
b1=new Button("black");
b2=new Button("pink");
b3=new Button("blue");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
addWindowListener(new W());
}
public void actionPerformed(ActionEvent e)
{
b1=(Button)e.getSource();
String s1=b1.getLabel();
if(s1.equals("black"))
{
setBackground(Color.black);
}
b2=(Button)e.getSource();
String s2=b2.getLabel();
if(s2.equals("pink"))
{
setBackground(Color.pink);
}
b3=(Button)e.getSource();
String s3=b3.getLabel();
if(s3.equals("blue"))
{
setBackground(Color.blue);
}
}
public class W extends WindowAdapter
{
public void WindowClosing(WindowEvent we)
{
if(a)
{
dispose();
}
else
{
System.exit(0);
}
}
}
public static void main(String args[])
{
colordemo cd = new colordemo("color");
cd.setSize(300,200);
cd.setVisible(true);
}}
Re: setBackground Color problem
Does the code do what you want it to do? If not, please explain.
Please edit the post and wrap the code in code tags. See:
BB Code List - Java Programming Forum