0

I am using NetBeans IDE and I have some problem when I am trying to change jLabel visibility to true:

private void buttonActionPerformed(java.awt.event.ActionEvent evt)
{
   LoaderLabel.setVisible(true);
   try { sleep(1000000); } catch { ... }
}

The visibility is changed only after the long sleep...

The problem is that I want to make some very intensive calculations in this method, but at the same time present some gif. Why the jLabel visibility is changed only at the end of the function and how do I fix it?

Thanks! :)

1 Answers1

1

This is because you should set the component's properties in EDT thread (Event Dispatch Thread). Try:

EventQueue.invokeLater(new Runnable() {
  @Override
  public void run() {
    LoaderLabel.setVisible(true);
  }
});

or using Lambda in Java 8

EventQueue.invokeLater(() -> LoaderLabel.setVisible(true));
Yasas
  • 82
  • 2
  • Thanks - but I didn't understand. I replaced my LoaderLabel.setVisible(true) with your code but nothing changed. –  Apr 21 '15 at 13:32
  • found it helpful - http://stackoverflow.com/questions/13238618/set-text-in-label-during-an-action-event –  Apr 21 '15 at 13:40