views:

26

answers:

1

Little background: I have a JTextField called reading. It is located on a JFrame panel.

reading = new JTextField("waiting for entry");

What I am trying to do is amend the JTextField to where it creates a blinking effect. Appears, and then disappears. I want that to alert the user that it needs attention.

So is there a way where I at a blink of an eye, its sometimes a blank string, sometimes showing 'waiting for entry':

reading.setText("           ");

reading.setText("Waiting for entry");

Any suggestions?

In the same class I have :

    public void test(){
    int delay = 300; //milliseconds
    ActionListener taskPerformer = new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
        reading.setVisible(!reading.isVisible()); } };
    Timer ok = new Timer(delay, taskPerformer);
    ok.start();

and let me show you my actionListener when reading gets the value:

public class MyChangeAction implements ChangeListener{
    public  void stateChanged(ChangeEvent ce){
      int value_MAP = slider.getValue();
      String str = Integer.toString(value_MAP);
      reading.setVisible(true); // I just added this 
      reading.setText("     MAP "+ str+" mm Hg ");
}}

So where do I type ok.stop(); or ok.cancel();

A: 

You could use a Timer and have

private class BlinkTask extends TimerTask
{
    public void run()
    {
        reading.setVisible(!reading.isVisible());
    }
}

registered to run every second or so.
Some Examples: link

Reese Moore
Ok I have implemented a swing timer. It is working.However, in the JFrame , I also have a slider. when the slider value is changed, the reading gets a number value. Basically I want the blinking to stop once the user has provided a number to the Jtext. Maybe I can be like "if (reading.StartsWith==" ") {stop the blinking } else { continue}
Faraz Khan
Keep a handel on the object that is accessible throughout the class. In the ActionListener method that gives reading its value, have a `reading.setVisible(true);` and a `timerVar.cancel();` which will cancel the blinking and make sure the JTextField is in the visible state.
Reese Moore
@Reese, I have edited my question, I have implemented reading.setVisible(true) but I am still having trouble implementing the time variable cancellation method.
Faraz Khan
@Faraz: You're going to want to keep the `Timer ok` declaration as a private variable for the class so that other methods can access it. Set it up in the constructor for instance. Then you can start it with `ok.start()` where you want to begin the blinking, and have an `ok.cancel()` call in your `stateChanged` method. This will stop the blinking when the slider is changed.
Reese Moore
Yes, I need to make it accessible within the stateChanged method. When I first defined it : Timer ok = new Timer(delay, taskPerformer)How do I tell it that its not limited within that method and can be used in the statechanged method as well?
Faraz Khan
@Faraz: To make a variable accessible throughout the class, declare the reference variable (i.e. `private Timer ok;`) in the class, then define it `ok = new Timer();` in some method. Once you've done that, the reference variable `ok` is available to any method or inner class within that class.
Reese Moore