Search code examples
javajtextfield

How to induce blinks on JTextField labels continously?


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();


Solution

  • 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