Obtain string path in Filechooser - java

I have a text editor program that opens up a file chooser whenever you click on Save in a Menu.
In order to achieve this saving process I create a file and save it with the current path in the file chooser.
// jfc = JFileChooser
String path = jfc.getSelectedFile().toString();
File testFile = new File(path);
This seems to work when I select an existing file in the file chooser, I.E "bio.txt" gets saved as a String in my program with the path "C:/Rikard/bio.txt"
But whenever I type in a new file name in the File name section and click Save, it doesn't save as a String and i get a NullPointerException, for Example Rikard.txt. It says in the method description that getSelectedFile() gets the file from user input by typing.
What could be wrong with my code?

Related

Plugin development: create new file dialog using FileDialog

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);
}

Problems with paste url into the JFile Chooser and hit enter

I'm using JFileChooser with Swing to get the input files. In the file open window, people will copy and paste the url into the "File Name:" and hit enter, then you will see all the files under that url folder. It was all working, but when you include the http: in that url, the file open window would close when hit enter, without letting you choose the files under that url folder and the address is not returned by the showOpenDialog method correctly.
I don't know what "http:" in the url will do to the JFileChooser, but my goal is to let the JFileChooser act like there is no "http:" in the url and display the spreadsheet file under that url folder.
Below is the code I used for the JFileChooser:
fc = new JFileChooser();
FileFilter xlsxExcelType = new FileNameExtensionFilter("Excel spreadsheet (.xlsx)", "xlsx");
fc.addChoosableFileFilter(xlsExcelType);
fc.addChoosableFileFilter(xlsxExcelType);
fc.setFileFilter(xlsExcelType);
fc.setCurrentDirectory(new java.io.File("."));
int returnVal = fc.showOpenDialog(CampaignTestFrame.this);

How to get directory path using JFileChooser by selecting a folder not a file

I want to use the JFileChooser in Java to get the user to select where they would like to save a file. I have used this code:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
This works and does get the path however the dialog box requires the user to select a file in order to get the file path. As i want it to save a new file i need it to get the path without having to select a file but from the folder the user has selected instead.
Im new to this and not sure of any other way of doing this, please could you advise me on a way around this.
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

Saving txt file to a user-defined location in Java SWT

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?

showing predefined file at File Chooser Option in Java Swing

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.

Categories