This a code written using java to delay the execution of the code by 5 seconds. But it is not working. either "this.jLabel2.setText("TDK");" statement not working. Can any one please help me to solve this problem.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jLabel2.setText("TDK");
boolean result=false;
result=stop();
if(result)
{
this.jLabel1.setText("MDK");
}
}
public boolean stop()
{
String current= new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()));
String future=new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()+5000));
while(!(current.equals(future)))
{
current= new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()));
}
return true;
}
You are blocking the event dispatch thread (no, don't use Thread.sleep() either). Use a swing Timer:
Timer timer = new Timer(HIGHLIGHT_TIME, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel1.setText("MDK");
}
});
timer.setRepeats(false);
timer.start();
where HIGHLIGHT_TIME
is the time you want to delay setting the text.