0

I got a simple code for downloading a file from a web page and I need to open another window that would show the downloading progress.

Here. Take a look at my files with code:

Gui.java

package com.app;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Gui extends JFrame {
    public static void main(String[] args){
        }
        JButton button;
        public Gui(){
            super("PAYDAY 2 Mod Installer");
            setLayout(new FlowLayout());

            button = new JButton("Install");
            add(button);

            GuiHandler handler = new GuiHandler();
            button.addActionListener(handler);
        }

        private class GuiHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                //System.out.println("test");
                GetModFiles files = new GetModFiles();
                try {
                    files.main(null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

GuiHandler.java

package com.app;

import javax.swing.JFrame;

public class GuiHandler {
    public static void main(String[] args) {
        Gui go = new Gui();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(350, 200);
        go.setAlwaysOnTop(true);
        go.setVisible(true);
    }
}

GetModFile.java

package com.app;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class GetModFiles {

    public static void main(String[] args) throws IOException {

        String fileName = "PAYDAY2.Pre-Alpha.0.06.zip";

        URL link = new URL("http://download1337.mediafire.com/wie8ek7gv53g/dy5vtumtbxyvt6h/PAYDAY2.Pre-Alpha.0.06.zip");

        InputStream in = new BufferedInputStream(link.openStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        while (-1 != (n = in.read(buf))) {
            out.write(buf, 0, n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();

        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(response);
        fos.close();
    }
}

Now how can I make it open a new window(after pressing the "Install" button) that would show the progress bar?

Tell me if you want more details.

Any ideas?

Tom Lenc
  • 765
  • 1
  • 17
  • 41
  • 1
    What part are you having trouble with? Have you read the [Swing Tutorial on progress bars](https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html), for example? – DNA Apr 21 '15 at 19:43
  • I've read it and I don't know how to make it work with downloading process.. Could you tell me how to do this please? – Tom Lenc Apr 21 '15 at 20:34
  • Thanks DNA! I got it working! Good thing you know where everything is ;). – Tom Lenc Apr 21 '15 at 22:08

0 Answers0