I'm reading a media with vlcj and I would like to display the time elapsed and the remaining time in some JLabels. I wrote some code but it seems my JLabel.setText don't refresh more that 2 times per second.
To make some more try and being sure that it's not the vlcj's thread that would have some troubles, I wrote a very code with a JLabel. The aim of this simple code is to update the JLabel 10 times per seconds.
Here is my code :
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestLabel extends JFrame implements Runnable{
JLabel label = new JLabel("0");
int i=0;
TestLabel() {
this.setTitle("Test");
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(label);
this.setVisible(true);
}
public static void main(String[] args) {
TestLabel tLabel = new TestLabel();
Thread t1 = new Thread(tLabel);
t1.start();
}
@Override
public void run() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
i+=1;
System.out.println(i);
label.setText(String.valueOf(i));
}
}, 0, 100, TimeUnit.MILLISECONDS);
}
}
Result : in the console, I get 1, 2, 3, 4... But in the JLabel, I have something like : 1, 2, 3 (...) 32, 37, 42, 47. It seems that the System.out.println write each "i", but the JLabel don't. Why do I have this artefact ?
Thank you for all your reply. Regards.