0

hi im creating a flashing text frame by using threading handling method, here is my code:

import javax.swing.*;

public class FlashingText extends JApplet implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel            jlblText         = new JLabel("welcome",JLabel.CENTER);

    public FlashingText() {
        add(jlblText);
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            while(true) {
                if(jlblText.getText() == null) {
                    jlblText.setText("Welcome");
                    Thread.sleep(2000);
                } else
                    jlblText.setText(null);
            }
        } catch(InterruptedException ex) {

        }
    }
}

after i compiled and ran it, it seems the text does not flashing at all is there anything wrong with my code? thanks a lot!

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
paradox
  • 258
  • 4
  • 19

3 Answers3

2

There's a better solution, which updates the UI in Event Dispatcher Thread and does not block it.

    final JLabel label = new JLabel("Some text");
    final Runnable updater = new Runnable() {
        @Override
        public void run() {
            label.setVisible(!label.isVisible());
        }
    };

    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    executorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            SwingUtilities.invokeLater(updater);
        }
    }, 2, 2, TimeUnit.SECONDS);
PKopachevsky
  • 262
  • 1
  • 6
1

From the code, it does not really seem that you are flashing anything. Some issues I see with your code:

  • If the label has no text, the getText() method will yield an empty string ("") and not null.
  • When updating visual components, you would need to go through the Event Dispatcher Thread (EDT). This is exposed to you through the SwingUtilities.invokeLater(Runnable runnable) class.
  • It is usually a bad idea to sleep() threads. If you make the changes through the EDT, you would be hanging the ED Thread which will cause the application UI to freeze, which is not desired.
  • You are swallowing exceptions. In your exception handling, you are not doing anything. It is considered bad practice to not handle exceptions (sometimes a simple log message will do).
npinti
  • 51,780
  • 5
  • 72
  • 96
0

According to me there is a problem in the following code block:

    try {
        while(true) {
            if(jlblText.getText() == null) {
                jlblText.setText("Welcome");
                Thread.sleep(2000);
            } else
                jlblText.setText(null);
        }
    }

Because see at the first time the value is welcome, so it will enter the loop and go to else and set it null and then immediately it will check again, as there is no sleep in else so it will check again and enter the if block and set it to welcome, and this whole process will be done at a great speed so you would not be able to see the flashing effect. So I think that you should try putting a sleep at the end of the else block and see, according to me it should work then. You should change:

 else
     jlblText.setText(null);

to

 else{
     jlblText.setText(null);
     Thread.sleep(500);
 }

or something like this

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71