Search code examples
javaswingprogress-barinputstreamjtextpane

Java: some sort of loadingtext whilst reading/writing to file (DL from URL)


I'm downloading a file from an URL and while this is going on I'd want to have some sort of loadingtext. No percentage is needed, just some indication that it's working with something.

    String zipTo = "src/client.zip";

    URL website = new URL(link);
    byte[] buffer = new byte[2048];
    int len;
    FileOutputStream out = new FileOutputStream(zipTo);
    InputStream in = website.openStream();
    in.read();

        while((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
    }

    out.flush();
    out.close();
    in.close();
    ZipHandler zh = new ZipHandler();
    zh.unZipFile(zipTo);

When the while-loop is going I'd like some loadingtext. I have a static JTextPane in my JFrame which acts as the status text. Something like "Loading . . . " (which has a sleeper between the dots).

Any tips?

Edit: I'm afraid I have very little experience with threads, and after reading about it I have noe clue how to implement it so it fits with my entire code.


Solution

  • You block EDT, because of your peogressText doesn't updating. You need to use SwingWorker for updating your status label.

    Read about Concurency in Swing and SwingWorker for background processes and updating UI from that.

    Here is a good example with SwingWorker and updating UI.