Java Swing How to - Schedual new thread for Swing








Question

We would like to know how to schedual new thread for Swing.

Answer

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/*from ww w. j  a v  a 2  s .  c  om*/
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);
    Executors.newScheduledThreadPool(1).scheduleAtFixedRate(()->{
        System.out.println(frame.getLocation());
    }, 0, 1000, TimeUnit.MILLISECONDS);
  }
}