You need to do two things. First of all you need to create another thread, that will work with data independently from the main thread. Secondly, you need to use SwingUtilities.invokeLater
method. This is a full example, that shows how it works. It changes label every second (Thread.sleep is used for delay in this example, don't use it in your real code).
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class NewMain {
// JFrame
static JFrame frame;
// JLabel
static JLabel label;
// Main class
public static void main(String[] args) {
frame = new JFrame("panel");
label = new JLabel();
// Creating a panel to add label
JPanel panel = new JPanel();
panel.add(label);
// Adding panel to frame
frame.add(panel);
// Setting the size of frame
frame.setSize(300, 300);
frame.show();
doLoop();
}
private static void doLoop() {
Thread newThread = new Thread(() -> {
while (true) {
SwingUtilities.invokeLater(() -> {
label.setText(new java.util.Date().toString());
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
newThread.start();
}
}