1

Is there a way to create a download button on java swing? I tried searching the internet for an answer but I only got links on how to create a download link on jsp/servlet

John
  • 836
  • 2
  • 25
  • 39

1 Answers1

6

Create a JButton whose ActionListeners ActionPerformed function performs the file download.

Here is a stackoverflow link on how to download files from a url with Java

How to download and save a file from Internet using Java?

Here is an example, not tested

...
JButton jb = new JButton('Download Button');
jb.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // Download file
        URL url = new URL('www.example.com/example_file.jpg');
        File f = new File('...');
        org.apache.commons.io.FileUtils.copyURLToFile(url, f);
    }
});
...
Community
  • 1
  • 1
calderonmluis
  • 547
  • 4
  • 11