I am trying to open dialog that ask for name of the new file and the place to put it, and then create new file in the right place.
But, save dialog (which is the most similar dialog I saw) save the file that opened.
But, I want that if I right click on file and I choose to save part of the file in another place, a save dialog will open and it's will save part of file in the chosen place (I have function that create the specs I want in the new file).
I tried this :
String[] FILTER_NAMES = { "Spectra Files (*.spectra)"};
// These filter extensions are used to filter which files are displayed.
String[] FILTER_EXTS = { "*.spectra"};
// User has selected to save a file
FileDialog dlg = new FileDialog(shell, SWT.SAVE);
dlg.setFilterNames(FILTER_NAMES);
dlg.setFilterExtensions(FILTER_EXTS);
String fn = dlg.open();
but, when I press on save, I want it to create the new file in calling the function:
createSpectraFile();
That is creating a new FILE with the content I want.
There is a way to do it?
Not sure if I understand your question right, but if you want to create a file from the result of the FileDialog do this:
String fn = dlg.open();
if (fn != null) {
createSpectraFile(fn);
}
Related
I am trying to open a JFileChooser dialog to let the user decide his wish-directory for following operations.
Following is my current code:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
chooser = new JFileChooser(home_dir);
}
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int retVal = chooser.showOpenDialog(frame);
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory().toString());
home_dir is a static String pointing to the users Download-directory.
The behaviour I do not understand:
home_dir = C:/Users/Scy/Downloads
Press OK without selecting any file(Or directory)
Output: C:/Users/Scy
home_dir = C:/Users/Scy/Downloads
Select any file within Downloads
Output: C:/Users/Scy/Downloads
Why do I not get the full-path(C:/Users/Scy/Downloads) as output when not selecting anything and just pressing OK? (With DIRECTORIES_ONLY activated, can't press OK without selecting anything without DIRECTORIES_ONLY)
Edit: I just noticed that when I just press the Cancel button without selecting anything the output is indeed what I expect, C:/Users/Scy/Downloads.
Based on an answer on this post I tried the following:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir)); //home_dir = "C:/Users/Scy/Downloads"
The result was exactly the same as above. Pressing the Cancel button results in the full path output, while pressing OK/Accept results in C:/Users/Scy.
Maybe the 'selected file'(or directory) is in the 'current directory' (which you are retrieving atm.)?
If you want the current selected file, chooser.getSelectedFile() is what you are looking for. Keep in mind that when switching to DirectoryOnly mode this method will return a directory (e.g. a File instance representing a directory).
The method chooser.getCurrentDirectory() will return the parent directory of the current selected file which explains the unexpected results. (getSelectedFile.getParentFile() will most likely return the same file)
If you are trying to retrieve the parentDirectory, you set the starting directory incorrect. Notice how you pass in the first constructor a selected File? This means in the second constructor the 'home_dir' will be the selected File. If you only want to set 'home_dir' as starting directory, you should use the no-args constructor and call chooser.setCurrentDirector(new File(home_dir)) instead. Here is a snippit of what your code could look like:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
// set last SELECTED file/directory path.
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
// set currentDirectory, but dont select anything yet.
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir));
}
getCurrentDirectory() returns a directory name, not a file name. If the user selects a file, this method returns the name of the directory which contains that file. If you want the name of the file which was selected, you should use getSelectedFile(). If you haven't yet, you should read this Oracle tutorial on file choosers.
This question already has an answer here:
SWT FileDialog: Selecting directories instead of files
(1 answer)
Closed 7 years ago.
I want to take folder path from user by using FileDialog. When user create a new folder then choose it and click "open", the folder open to choose new files in it. but, I want to take path of the choosen new folder without open it. My example code is below:
// File standard dialog
FileDialog fileDialog = new FileDialog(shell);
// Set the text
fileDialog.setText("Select File");
// Set filter on .txt files
fileDialog.setFilterExtensions(new String[] { "*.txt" });
// Put in a readable name for the filter
fileDialog.setFilterNames(new String[] { "Textfiles(*.txt)" });
// Open Dialog and save result of selection
String selected = fileDialog.open();
System.out.println(selected);
Can anybody give some advice about it? Thank you.
If you want to let the user choose a directory you can use the DirectoryDialog instead of a FileDialog.
I am making a simple java application using SWT GUI to save a txt file to a user defined location. When a button is clicked, I open the directories like this:
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
directoryDialog.setFilterPath(selectedDir);
directoryDialog.setMessage("Please select a directory and click OK");
String dir = directoryDialog.open();
Now I need to be able to make a file and save it to that location. How can I make and save to the user given location?
How can I arbitrarily display the same file dialog that occurs from New -> File?
I have an Eclipse Action where I wish to display the project file path dialog, and not the system file path dialog, as seen in this image:
Also there is one catch: I want to display existing files too, as I will not be creating a new file, but instead may be overwriting/synchronizing a file. If this is NOT possible, I'll still want to know how to just display the same dialog as is in New -> File.
To display a workspace file picker, you will need to do something along these lines:
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
shell,
new WorkbenchLabelProvider(),
new WorkbenchContentProvider());
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.setAllowMultiple(false);
if (dialog.open() == Window.OK) {
IResource resource = (IResource) dialog.getFirstResult();
}
ElementTreeSelectionDialog is quite customizable, so you can tune the behavior to your needs.
So, i am showing a file chooser dialog using:
JFileChooser c = new JFileChooser();
int rVal = c.showSaveDialog(this);
if (rVal == JFileChooser.APPROVE_OPTION) {
saveFile((c.getCurrentDirectory().toString() + "/" + c.getSelectedFile().getName()), "data to be saved");
}
It works fine, but problem is user himself has to write a file name at the file prompt where file name is to be provided or he can select a file from file listing so that it can be overwritten.
I want to know is there any way that the file chooser text box already shows the users suggested file name so that he simply clicks on save and it works.
Just call setSelectedFile method before showing the dialog.