Solved

5 second color change

Posted on 2004-09-17
24
304 Views
Last Modified: 2010-03-31
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 : http://www.experts-exchange.com/Programming/Q_21135331.html

thank you
0
Comment
Question by:Samooramad
  • 10
  • 8
  • 6
24 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
Comment Utility
new Thread(new Runnable()
{
   public void run()
   {
      Color orig = label.getColor();
      label.setText(Color.green);
      try
      {
         Thread.sleep(5000);
      }
      catch (Exception ex)
      {}
      label.setColor(orig);
   }
});
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
Comment Utility
You could do something like:


                  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("Setting color...");
                              if (label.isOpaque() == false) {
                                    label.setOpaque(true);
                              }
                              label.setBackground(toColor);
                              try {
                                    Thread.currentThread().sleep(delay);
                              }
                              catch(Exception e) {
                                    /* ignore */
                              }
                              System.out.println("Restoring color...");
                              label.setBackground(fromColor);
                        }
                  }


      public void actionPerformed(ActionEvent e) {
            try {
                  new Thread(new ColorSetter(yourLabel, Color.red, 5000)).start();
            }
            catch(Exception ex) {
                  ex.printStackTrace();
            }

      }

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(Although how you do it depends on what thread you want to do this from)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
>     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.
0
 

Author Comment

by:Samooramad
Comment Utility
>>(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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
In what context of your application are you setting the color?
0
 

Author Comment

by:Samooramad
Comment Utility
sorry I still dont understand CEHJ :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
From where do you intend to do this?
0
 

Author Comment

by:Samooramad
Comment Utility
labels on a panel in an internal frame.. is that what you meant?
0
 

Author Comment

by:Samooramad
Comment Utility
why did you use delay CEHJ?
what does it delay exactly?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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(ActionEvent evt)
   {
         L.setColor(C);
   }
}
0
 

Author Comment

by:Samooramad
Comment Utility
objects in your code.. do i need to call run somehow to activate the thread?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
if you have morew than one label then you could enhance it to use an array of labels
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> do i need to call run somehow to activate the thread?

no
0
 

Author Comment

by:Samooramad
Comment Utility
how could I change the code to pass the labels as parameters?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
to change a label (and have it change back) you would use:

new Timer(5000, new Changer(label, Color.green)).start();

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>
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
0
 

Author Comment

by:Samooramad
Comment Utility
I don't know what I did wrong objects but it didnt work.. I didnt get any errors
0
 

Author Comment

by:Samooramad
Comment Utility
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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(JFrame.EXIT_ON_CLOSE);
            yourLabel = new JLabel("Save image");
            getContentPane().add(yourLabel, 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("Setting color...");
                        if (label.isOpaque() == false) {
                              label.setOpaque(true);
                        }
                        label.setBackground(toColor);
                        try {
                              Thread.currentThread().sleep(delay);
                        }
                        catch(Exception e) {
                              /* ignore */
                        }
                        System.out.println("Restoring color...");
                        label.setBackground(fromColor);
                  }
            }


      public void actionPerformed(ActionEvent 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);
      }

}
0
 

Author Comment

by:Samooramad
Comment Utility
hey CEHJ what does setopaque do?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
It makes the component opaque. It it's not opaque and you set its Color, you won't notice any difference
0
 

Author Comment

by:Samooramad
Comment Utility
oh.. maybe thats why I could get objects code to work :)

yours worked great!

thank you
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 72
groovy example issue 10 65
Strange loading of website behaviour 3 23
Java JRE greater than 1.6 5 13
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now