Problems with paste url into the JFile Chooser and hit enter - java

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

Related

Obtain string path in Filechooser

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?

How to modify the default Save / SaveAs path to our defined path on any browser’s Save/SaveAs pop up functionality?

On a button click, I am downloading the excel sheet to a specific folder. The file is successfully saved.
Code is something like this :
Workbook wb = initWorksheet(form);
String exportFilePath =ResourceBundle.getBundle("Test").getString("Test_export_file_path"); //file path from properties file
FileOutputStream fileOut = new FileOutputStream(exportFilePath + "/" + getNewFileName(fileName, exportFilePath));
wb.write(fileOut); // sheet is successfully downloaded at folder specified.
output = fileOut.toString();
fileOut.close();
(HttpServletResponse response is send successfully and Save/SaveAs pop appears !)
But, when I click Save button on browser’s Save pop-up, excel file with same name and extension is downloaded in System’s Download folder (But this file is corrupted and we cannot open it)
So what I want is, even if I click Save in browser’s pop-up, I want the file to get download at the same path as mentioned in my properties file (here exportFilePath path)
Can anybody please help me in it !
You cannot change the place where the user browser will save the downloads.

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

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.

jfilechooser - set directory to a path in a file

I am trying to set the directory path in JFilechooser through something like this(using commons-io ) :
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File theDirectory = new File(fileContents);
filechooser = new JFileChooser();
fileChooser.setCurrentDirectory(theDirectory);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I'm using getCanonicalPath() to get the path and write in the file path.txt
path = file.getCanonicalPath();
I don't intend to put all my code here,but I'm sure that the program writes and reads the path in path.txt.
I don't get any error,but everytime I run the program it always open JFilechooser in my documents folder.What i am doing wrong?
Try to pass the current directory directly in the constructor:
filechooser = new JFileChooser(theDirectory);
If you consult the API, using the default constructor (i.e. new JFileChooser()):
Constructs a JFileChooser pointing to
the user's default directory. This
default depends on the operating
system. It is typically the "My
Documents" folder on Windows, and the
user's home directory on Unix.
This would seem to account for always opening to My Documents, but this isn't your problem. In fact, your problem lies with setting the current directory (i.e. setCurrentDirectory(theDirectory)):
Sets the current directory. Passing in
null sets the file chooser to point to
the user's default directory. This
default depends on the operating
system. It is typically the "My
Documents" folder on Windows, and the
user's home directory on Unix. If the
file passed in as currentDirectory is
not a directory, the parent of the
file will be used as the
currentDirectory. If the parent is not
traversable, then it will walk up the
parent tree until it finds a
traversable directory, or hits the
root of the file system.
That being said, I'd pay attention to the highlighted text since it appears that you're setting a file as the current directory and not a directory.
In your main class declare
public static String dirpath=".";
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser(dirpath);
dirpath =jfc.getSelectedFile().getAbsolutePath().toString();
}
For select the last directory that you open :
chooser.setCurrentDirectory(lastDirectory);
int r = chooser.showOpenDialog(new JPanel());
if (r == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getPath();
lastDirectory = chooser.getSelectedFile();
}
JFileChooser Chooser = new JFileChooser("F:");
if you want to change the directory theb use System.getProperty method
String s=System.getProperty("user.dir"); // changes directory from documents to the user current Directory;
JFileChooser jfc=new JFileChooser(s);

Categories