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
Asked
Active
Viewed 6,382 times
1
-
Applets are not really meant to be a delivery system for files, it sort of renders their security measures useless. – thatidiotguy Nov 02 '12 at 15:03
-
thank you for your reply. not pertaining to applets though. I am talking about a desktop application using java swing. – John Nov 02 '12 at 15:07
-
You would need to create a regular java button(JButton), that when clicked calls a function which does the download. – calderonmluis Nov 02 '12 at 15:11
-
Apologies for my applet assumptions. See others comments and answers. – thatidiotguy Nov 02 '12 at 15:14
1 Answers
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