Samooramad
asked on
5 second color change
Hi experts,
How do I make a labels text change color for 5 seconds then go back to black.
also, if someone could help me.. I accidentally asked a question outside the java topic area. Either tell me how to change it or take a look at it to solve : https://www.experts-exchange.com/questions/21135331/saving-and-opening.html
thank you
How do I make a labels text change color for 5 seconds then go back to black.
also, if someone could help me.. I accidentally asked a question outside the java topic area. Either tell me how to change it or take a look at it to solve : https://www.experts-exchange.com/questions/21135331/saving-and-opening.html
thank you
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(Although how you do it depends on what thread you want to do this from)
> label.setText(Color.green) ;
that should of cource be setForeground() not setText() :)
same when resetting it
Use setBackground() if you want to change the background colour.
that should of cource be setForeground() not setText() :)
same when resetting it
Use setBackground() if you want to change the background colour.
ASKER
>>(Although how you do it depends on what thread you want to do this from)
dont have any experience with them so can't answer that
dont have any experience with them so can't answer that
In what context of your application are you setting the color?
ASKER
sorry I still dont understand CEHJ :)
From where do you intend to do this?
ASKER
labels on a panel in an internal frame.. is that what you meant?
ASKER
why did you use delay CEHJ?
what does it delay exactly?
what does it delay exactly?
you can use the following class like so:
new Timer(5000, new Changer(label, Color.green)).start();
public class Changer implerments ActionListener
{
private JLabel L;
private Color C;
public Changer(JLabel l, Color c)
{
L = l;
C = label.getColor();
l.setColor(c);
}
public void actionPerformed(ActionEven t evt)
{
L.setColor(C);
}
}
new Timer(5000, new Changer(label, Color.green)).start();
public class Changer implerments ActionListener
{
private JLabel L;
private Color C;
public Changer(JLabel l, Color c)
{
L = l;
C = label.getColor();
l.setColor(c);
}
public void actionPerformed(ActionEven
{
L.setColor(C);
}
}
ASKER
objects in your code.. do i need to call run somehow to activate the thread?
if you have morew than one label then you could enhance it to use an array of labels
> do i need to call run somehow to activate the thread?
no
no
ASKER
how could I change the code to pass the labels as parameters?
to change a label (and have it change back) you would use:
new Timer(5000, new Changer(label, Color.green)).start();
new Timer(5000, new Changer(label, Color.green)).start();
>>
why did you use delay CEHJ?
what does it delay exactly?
>>
It delays the change of Color
>>how could I change the code to pass the labels as parameters?
The code i posted already does that
>>How do I make a labels text change color for 5 seconds then go back to black.
The code i posted does just that. Since you didn't specify you want this to happen repeatedly, it simply happens once
why did you use delay CEHJ?
what does it delay exactly?
>>
It delays the change of Color
>>how could I change the code to pass the labels as parameters?
The code i posted already does that
>>How do I make a labels text change color for 5 seconds then go back to black.
The code i posted does just that. Since you didn't specify you want this to happen repeatedly, it simply happens once
ASKER
I don't know what I did wrong objects but it didnt work.. I didnt get any errors
ASKER
>>The code i posted does just that. Since you didn't specify you want this to happen repeatedly, it simply happens once
yes I only want it once.. Ok give me some time and I'll get back to you CEHJ. I'm working on it
yes I only want it once.. Ok give me some time and I'll get back to you CEHJ. I'm working on it
Runnable example (although you can delete nearly all of the imports ;-)):
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
public class F extends JFrame implements ActionListener {
JEditorPane editorPane;
JLabel yourLabel;
private void setGui() {
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
yourLabel = new JLabel("Save image");
getContentPane().add(yourL abel, BorderLayout.CENTER);
JButton b = new JButton("Change color");
b.addActionListener(this);
getContentPane().add(b, BorderLayout.SOUTH);
}
class ColorSetter implements Runnable {
JLabel label;
Color toColor;
Color fromColor;
int delay;
public ColorSetter(JLabel label, Color toColor, int delay) {
fromColor = label.getBackground();
this.label = label;
this.toColor = toColor;
this.delay = delay; // in milliseconds
}
public void run() {
System.out.println("Settin g color...");
if (label.isOpaque() == false) {
label.setOpaque(true);
}
label.setBackground(toColo r);
try {
Thread.currentThread().sle ep(delay);
}
catch(Exception e) {
/* ignore */
}
System.out.println("Restor ing color...");
label.setBackground(fromCo lor);
}
}
public void actionPerformed(ActionEven t e) {
try {
new Thread(new ColorSetter(yourLabel, Color.red, 5000)).start();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
F f = new F();
f.setGui();
f.pack();
f.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
public class F extends JFrame implements ActionListener {
JEditorPane editorPane;
JLabel yourLabel;
private void setGui() {
setDefaultCloseOperation(J
yourLabel = new JLabel("Save image");
getContentPane().add(yourL
JButton b = new JButton("Change color");
b.addActionListener(this);
getContentPane().add(b, BorderLayout.SOUTH);
}
class ColorSetter implements Runnable {
JLabel label;
Color toColor;
Color fromColor;
int delay;
public ColorSetter(JLabel label, Color toColor, int delay) {
fromColor = label.getBackground();
this.label = label;
this.toColor = toColor;
this.delay = delay; // in milliseconds
}
public void run() {
System.out.println("Settin
if (label.isOpaque() == false) {
label.setOpaque(true);
}
label.setBackground(toColo
try {
Thread.currentThread().sle
}
catch(Exception e) {
/* ignore */
}
System.out.println("Restor
label.setBackground(fromCo
}
}
public void actionPerformed(ActionEven
try {
new Thread(new ColorSetter(yourLabel, Color.red, 5000)).start();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
F f = new F();
f.setGui();
f.pack();
f.setVisible(true);
}
}
ASKER
hey CEHJ what does setopaque do?
It makes the component opaque. It it's not opaque and you set its Color, you won't notice any difference
ASKER
oh.. maybe thats why I could get objects code to work :)
yours worked great!
thank you
yours worked great!
thank you
8-)