Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In the simplest way, you could have something like this:</p> <pre><code>import javax.swing.JFrame; public class TestBackgroudMethod { public static void main(final String[] args) { MyBackgroudMethod thread = new MyBackgroudMethod(); thread.setDaemon(true); thread.start(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JFrame jFrame = new JFrame(); jFrame.setSize(200, 200); jFrame.setVisible(true); } }); } public static class MyBackgroudMethod extends Thread { @Override public void run() { while (true) { System.out.println("Executed!"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } </code></pre> <p><strong>Edit</strong></p> <hr> <p>Added sample with swing worker:</p> <pre><code>import javax.swing.JFrame; import javax.swing.SwingWorker; public class TestBackgroudMethod { public static void main(final String[] args) { new SwingBackgroupWorker().execute(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JFrame jFrame = new JFrame(); jFrame.setSize(200, 200); jFrame.setVisible(true); } }); } public static class SwingBackgroupWorker extends SwingWorker&lt;Object, Object&gt; { @Override protected Object doInBackground() throws Exception { while (true) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { System.out.println("executed"); // here the swing update } }); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload