Reputation: 799
I have a settings tab in my program. The data you can set there, is not only changeable from this panel. That's why I want to reload this data like every 5 seconds. I think this has to be done with an extra Thread, but my knowledge about Threads is minimal. I already have a reload method for this purpose.
What should I use to do this (and how...)?
Upvotes: 5
Views: 765
Reputation: 285403
but my knowledge about Threads is minimal...
paintComponent(...)
method, or perhaps better, in a BufferedImage that is then displayed inside of paintComponent(...)
.ScheduledExecutorService
as per syb0rg's answer (1+ to syb0rg's answer) that requests and obtains new data every 5 seconds.repaint()
from the Swing event thread after your data is changed. If using a SwingWorker, the process/publish method pair could help with this. You could publish your data to the Swing event thread with this.Upvotes: 5
Reputation: 8247
Use a ScheduledExecutorService:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(yourRunnable, 5, 5, SECONDS);
Then reload your JPanel
in yourRunnable
(just follow the example from the JavaDocs).
Upvotes: 5