How to give user choose folder option while downloading file in javaFX - java

I am building a desktop app using javafx. I need to download a file from FTP.
I want before downloading the user should be prompted with the windows/mac explorer window to choose the download location.
How can I achieve this in Javafx ??
I am downloading the file on the click of an button so I am using it inside my controller class.

You can choose a file with a FileChooser or choose a directory with a DirectoryChooser.
E.g.
DirectoryChooser dirChooser = new DirectoryChooser();
File chosenDir = dirChooser.showDialog(primaryStage);

Have you tried DirectoryChoser?
It opens an OS native dialog to select a directory, and returns it as a File object.
If you want to create a new file, you can read the Path of the selected directory, append a file name and create the new File object you want to save.
For example:
DirectoryChooser dirChooser = new DirectoryChooser();
dirChooser.setTitle("Select a folder");
File selectedDir = dirChooser.showDialog(primaryStage);
String selectedDirPath = dirChooser.showDialog(mainApp.getPrimaryStage()).getAbsolutePath();
File downloadedFile = new File(selectedDirPath + "/" + downloadedFileName);

Related

How can I open a file from the explorer and copy to a directory?

I have a little problem,
I need to do a program that open the file explorer and copy the selected file in a specified directory.
I've only found how to open the explorer :
File file = new File ("c:\\<directory>");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
but I need to get the selected file and copy in a default directory, and I really don't know how to do it.
Thanks a lot!!
I assume you have a SWT Java Application. Then you can use FileDialog to show a file chooser where the user selects a file. Without a GUI it wouldn't be easy to show a file choosing dialog to the user.
This code snippet uses FileUtils from Apache Commons IO:
FileDialog dlg = new FileDialog(frame, "Choose a file to copy", FileDialog.OPEN);
dlg.setVisible(true); // blocks until user completes the action
String fileToCopyString = dlg.getFile();
if (fileToCopyString != null) {
File fileToCopy = new File(fileToCopyString);
if (fileToCopy.isFile()) {
FileUtils.copyFile(fileToCopy, new File(tmpDir, fileToCopy.getName());
}
}
References:
FileDialog Javadoc
FileUtils Javadoc
How to use FileDialog?
Try read the file and write it to another directory. You can use FileReader() and FileWriter() methods.

How to set file path to src folder of project

I want to make a program that you can email to someone and they can run it.
Right now my code for making a file is like this:
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();
But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?
Leave the path off entirely, it will use the directory of the project.
Change
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");
To
File f = new File("axmlfile.xml");
I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.
try{
//create a temporary file
File temp = File.createTempFile("axmlfile", ".xml");
System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}

Save file dialog in griffon

i have a save file dialog in my griffon app. the code is like this:
String filename = siteCode+'-'+model.currentClient.name+'-'+computerName+'-'+date+'.csv'
File selFile = new File(filename)
def fc = new JFileChooser()
fc.setSelectedFile(selFile)
fc.showSaveDialog()
BufferedWriter out = new BufferedWriter(new FileWriter(selFile));
The problem is, regardless the path i choose to save file at, it alsways goes to the "Staging" directory in my app's folder.
Could it be that you're not reading back the selected file from the dialog but reusing the selFile variable? Make sure you call fc.getSelectedFile() instead.

JFileChooser returns incorrect path in OS X (folders only mode)

I have a problem in java swing where the user has to select a folder, so I am using the code below.
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(fc.showDialog(singleton, SELECT) == JFileChooser.APPROVE_OPTION) {
File folder = fc.getSelectedFile();
String path = folder.getPath() + File.separatorChar + MYAPPFOLDER;
}
Now there are 2 ways a user may select the folder
Navigate to the folder and select the folder
Navigate to the folder, go into the folder, and click select
Both ways work fine on windows but on OS X, I get
If I do 1 : path = Users/<username>/Desktop/MYAPPFOLDER
If I do 2 : path = Users/<username>/Desktop/Desktop/MYAPPFOLDER
How do I avoid this 2nd case?
Thanks in advance.
The problem is that showDialog doesn't know whether this is a load or save operation, so it gives you the textbox to put the new file/folder name in. This is set to 'Desktop' when you click on the folder to go into it (as the first click of a double-click) and if the user then presses SELECT, the dialog assumes you want to create a new folder with that name and returns it in the path.
One solution is to use the showOpenDialog call instead, and manually change the chooser's title and approve buttons to SELECT. That way, the user never sees the new directory textbox.
The code would look something like this:
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("Select a folder");
fc.setApproveButtonText(SELECT);
if(fc.showOpenDialog(singleton) == JFileChooser.APPROVE_OPTION) {
File folder = fc.getSelectedFile();
String path = folder.getPath() + File.separatorChar + "MYAPPFOLDER";
}

Java File manipulation

So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.
You choose a file like this:
File fileToRead = JFileChooser.getSelectedFile();
Then you read and change the content and write it back to the same location with a different name:
File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );

Categories

Resources