-2

I am creating a progressBar using JLabel. Problem is when I add this label to my frame and execute some other tasks, my JFrame freezes. Is there any method like invoke and wait for swing to complete its drawing?

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {
   JLabel loadingBar = new JLabel("test");
   myFrame.add(loadingBar);
   doSomeOtherTasks();
}
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • 1
    Can you please show a [mcve]? This code alone shouldn't freeze – OneCricketeer Jun 08 '17 at 11:56
  • 1
    When your UI freezes, you are doing something wrong around the https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – GhostCat Jun 08 '17 at 11:58
  • 1
    It's not so much a matter of forcing Java to update the label as not blocking the Event Dispatch Thread from doing so by performing a long running task on the EDT. To put that another way: Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Jun 08 '17 at 11:58
  • @AndrewThompson I dont want to block EDT but I want to update GUI before long running function. – hellzone Jun 08 '17 at 12:01
  • 1
    So implement my suggestion! I find it hard to believe you *tried what I suggested* in the 4 minutes since I posted that comment. – Andrew Thompson Jun 08 '17 at 12:03

1 Answers1

2

Try this

int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();
Harisudhan. A
  • 662
  • 1
  • 6
  • 20