1

Hey i just need a question answered... How would i make the following code not freeze my whole JFrame?

                try {
                Thread.sleep(Integer.parseInt(delayField.getText()) * 1000);
                System.out.println("Hello!");
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
user2413200
  • 245
  • 3
  • 7
  • 12

4 Answers4

8

use a different thread to perform this task. If you do this in the main UI thread then it will freeze.. For example you can do following

  new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(Integer.parseInt(delayField.getText()) * 1000);
                System.out.println("Hello!");
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

        }
    }.start();

UPDATE

AFter wise suggestions of Robin and Marko I am updating the answer with a better solution.

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
                System.out.println("Hello!");

        }
    };
    javax.swing.Timer t = new javax.swing.Timer(Integer.parseInt(delayField.getText()) * 1000, taskPerformer);
    t.setRepeats(false);
    t.start();
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 2
    Since `JTextField#getText` is not thread-safe, you shouldn't be calling that method from another thread then the EDT – Robin May 28 '13 at 10:19
  • @Robin, yup didn't think about that while postin :(... a synchronized method can be used to get the text.. will there be any side affect of that? – stinepike May 28 '13 at 10:21
  • I would retrieve the text outside of that thread, and simply pass it in there. And simply for delay, I would ditch the thread completely and use a Timer as suggested by Marko Topolnik – Robin May 28 '13 at 10:29
  • Thanks @Robin for enlighting me .. I am updating the answer accorind to your advice – stinepike May 28 '13 at 10:34
3

Whenever you are about to use Thread.sleep in your GUI code, stop yourself and think of Swing Timer, which is the right tool for the job. Schedule the task you need to perform with a delay.

Using another thread for this is not the best advice: it wastes a heavy system resource (a thread) to do absolutely nothing but wait.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

This is not the correct way to use threads in java . You should use swingutilities.invokelater

swing utils invoke later

user1889970
  • 726
  • 1
  • 8
  • 12
1

You don't want to execute this on the UI (or event dispatch thread) thread. Rather in a separate thread. Otherwise (as you've seen) you'll block the UI.

It's a good practice to perform time-consuming operations on a separate thread, and make use of SwingUtilities.invokeLater() if those threads need to perform some subsequent UI action (e.g. in the above display "Hello" in the UI)

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440