简体   繁体   中英

Java C/S Application preloader (like Ajax)

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?

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);                               
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM