1

I would like to implement a "wait" preloading message in a Java desktop application (I use Swing), like the preloaders the Ajax/Flash developers use in web applications.

My application uses a client/server architecture, and there may be some delay when moving from window to window, due to the response time of the server, but the user should not be able to do anything until all the content is loaded.

What's the best practice to do this? I had 2 ideas, but I think there should be a better way to handle this:

1 idea) Blocking the Event Dispatcher Thread (is that even possible?)

alternative idea) Hide the whole JFrame until the message exchange with the server is completed and the content is ready, showing another "wait" JFrame, but I dont really like this solution, because the main JFrame would keep flashing on/off during the user interaction

EDIT: Thanks to some hints I figured it out, but I can't give the answer to anyone (nobody actually answered, i have only comments!), should i vote to delete the question?

alessiop86
  • 1,285
  • 2
  • 19
  • 38
  • 2
    (block EDT) *" (is that even possible?)"* Easily, but don't. Show a modal dialog instead. – Andrew Thompson Sep 09 '12 at 07:56
  • @AndrewThompson But with a modal dialog, if the JFrame is still displayed on background, the user can click somewhere, messing up with the application logic (I know, it's not very robust, but it's supposed to be very simple) – alessiop86 Sep 09 '12 at 08:00
  • 2
    *" the user can click somewhere,"* They can click anywhere they like. They can click the dialog to bring it & the app. to the front, or click anywhere in the frame to hear a pleasant little 'ding' that says "don't click this". – Andrew Thompson Sep 09 '12 at 08:17
  • 1
    yes, like @AndrewThompson suggested, showing a `JDialog` as long as the frame (in the background) is not ready is your best bet. Don't allow the user to close the dialog until then. – asgs Sep 09 '12 at 08:28
  • Yes, but I don't know how to implement the actionlistener that targets the whole JFrame – alessiop86 Sep 09 '12 at 08:30
  • What 'action listener'? Answer that with an [SSCCE](http://sscce.org/) ..and some sentences, if needed. Note also that now @asgs has commented as well, you **will** need to add the '@'PersonName notation to ensure they find out a new comment has been made. – Andrew Thompson Sep 09 '12 at 08:33
  • 1
    Here's a minimal [example](http://stackoverflow.com/a/4530659/230513) that deals with uncertain latency. You can [answer your own question](http://meta.stackexchange.com/q/17463/163188) or edit your question to include an [sscce](http://sscce.org/). – trashgod Sep 09 '12 at 12:55
  • If you've solved the problem yourself, please post your solution as an answer below. Thanks. – Bill the Lizard Sep 09 '12 at 13:21

1 Answers1

1

I solved the issue using a JDialog, playing with setVisible(true/false) and denying the possibility for the user to close the dialog by pressing the X button, using setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

public class PreloaderDialog extends JDialog {              

        publicPreloaderDialog() {

                initialize();
        }

        public final void initialize() {

                JLabel waittext = new JLabel("Wait, please");    
                add(waittext);

                setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
                setModalityType(ModalityType.APPLICATION_MODAL);

                setTitle("Loading in progress");

                setLocationRelativeTo(null);
                setSize(300, 120);
        }

        public void hide() {

           setVisible(false);                               
        }

        public void show() {                                       

            setVisible(true);                               
        }

}
alessiop86
  • 1,285
  • 2
  • 19
  • 38