简体   繁体   中英

Displaying loading jLabel before sending a HTTP request

I have a jLabel with the content "Loading...pelase wait!" . Its visibility has been set to false. I implemented the following code to make it visible before sending the HTTP request and hiding it when the request has been completed.

    Loading.setVisible(true);
    loaddata();
    Loading.setVisible(false);

The loaddata function does all to load HTTP data using HTTP GET. The content is received perfectly fine. But the Loading label does not show up. However, when I implement this code:-

    Loading.setVisible(true);
    loaddata();

The loading jLabel shows up(But does not go away for obvoious reasons). Can anyone please tell me how to implement this successfully?

You are executing a long running task on the Event Dispatch Thread and that task is preventing the GUI from repainting itself.

Read the section from the Swing tutorial on Concurrency . The solution is to use a SwingWorker so you don't block the EDT.

Your HTTP request is asynchronous, so you should disable your Loading indicator when your request will be completed.

Loading.setVisible(true);
loaddata();
Loading.setVisible(false);

These code make visible you indicator, send request (will be created new thread), and perform next operation: make invisible indicator (so you think you loading indicator was never showing, but it was).

Make flag which will be setted, when request will be completed, or try implement Listener pattern for your request method, you can found example here Listener(Observer) .

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