-1

I am creating an object of Downloader JFrame class in the constructor of the main form and calling its setVisiblity(true) method on download button click.

The problem is Downloader frame is showing but when the method has termininated:

 Downloading dn = new Downloading();

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        dn.setVisible(true);  
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(GehuConnectMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

The form is showing after 5 second what is the solution ?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Rishabh Gusain
  • 683
  • 1
  • 6
  • 23
  • 1) 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. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 04 '17 at 14:10
  • what should I do to show a waiting form until the another class is downloading the contents? – Rishabh Gusain Apr 04 '17 at 14:13
  • The answer is in the link you obviously could not have properly read within the space of 3 minutes. – Andrew Thompson Apr 04 '17 at 14:20
  • This sort of problem has been asked many times previously. Please search first before asking what is likely to be a common problem. – Hovercraft Full Of Eels Apr 04 '17 at 14:21

1 Answers1

2

Simple:

Thread.sleep(5000);

You are sleeping on the Event Dispatcher Thread. That will freeze your whole application. You have to step back and look into invokeLater to ensure "correct" threading within your UI.

Or, maybe more appropriate: why do you intend to sleep in the first place. The user clicks that button; and you raise that other frame. If you now have to "wait" for something else; then that needs to happen "in a different" way.

One typical answer would be to use a modal dialog for example. Don't try to re-invent the wheel here.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248