JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = chooser.showOpenDialog(this);
if(returnValue == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
filechooser.getSelectedFile().getAbsolutePath());
this is the code i used to open a file and get its path printed but the thing is i want to get the path of a exe file which means path should end up with the file extension at the end. with the current code it won't even show the exe files.
If you need exe files, you can use a Filter, but with the good options, like that (your code is Directories oriented ):
JFrame frame=new JFrame();
JFileChooser filechooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("EXE File","exe");
filechooser.setFileFilter(filter);
filechooser.showOpenDialog(frame);
File file = filechooser.getSelectedFile();
System.out.println("YOU CHOOSE "+file.getAbsolutePath());
a usefull link on that question: FileFilter for JFileChooser
see this for the option filechooser.setFileSelectionMode:
JFileChooser select directory but show files
Of course it won't show files, you're using
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Related
I'm trying to filter file types in my JFileChooser like this;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(langManager.getText("programwidget.urlbutton.fd.text"));
fc.setFileFilter(new FileNameExtensionFilter(null, "ico", "png", "gif"));
But when I launch it, I can still choose another file types, so it's not working as I expected.
Can someone please tell me what I'm missing here?
I am doing a project for vehicle repair center. In this project I need to upload photos of the damaged vehicle so employees can watch them later.
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filePath = f.getAbsolutePath();
path.setText(filePath);
File imgFile = new File(filePath);
try {
FileInputStream fin = new FileInputStream(imgFile);
I tried above, but I can choose only one photo at one time, like this:
I need these kind of thin
ddddd
i don't do java but a quick glance at the manual suggests you need to enable selection of multiple files:
setMultiSelectionEnabled
public void setMultiSelectionEnabled(boolean b)
Sets the file chooser to allow multiple file selections.
Parameters:
b - true if multiple files may be selected
See Also:
isMultiSelectionEnabled()
https://docs.oracle.com/javase/8/docs/api/javax/swing/JFileChooser.html#setMultiSelectionEnabled-boolean-
For my ongoing project at work I have been asked to add a feature to my program. Currently the program allows a user to upload a file which is then manipulated. After being reformatted the file is saved into a new .ACH file (which is just a text file with strict formatting standards).
Currently the user is able to upload an ACH file from anywhere on their computer using this code:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
chooser.setCurrentDirectory(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
Later in the program buffered reader/writer make edits to the file and then save it to the new location using this code:
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
Finally, the user is informed of the file name and save location using this code:
JOptionPane.showMessageDialog(null, "Output file saved as NachaOutput.ach to " + chooser.getCurrentDirectory());
As you can see, the new file is saved to the same directory that the file was uploaded from. My manager has requested that I allow the user the choice of a directory to save the new file to. (The file name is not imporant nor does it need to be set by the user, only the directory).
I have tried and tried to research how to do this, to no avail. The only guide that I found was very lengthy and I'll confess was too complicated for me to understand. I have attempted to create a new JFileChooser and allow the user to set the directory only and use the code:
int saveVal = chooser2.showSaveDialog(chooser2);
However I find that when I attempt to pull the saved directory later like this:
bw = new BufferedWriter(new FileWriter(chooser2.getCurrentDirectory()+"//NachaOutput.ACH"
the file is saved to my computers standard directory. (C://Users or something to that affect.)
Is there a basic way that I can give the user a simple input to select a directory and then utilize it with the bw on the line I shown above?
Thanks in advance.
Is there a basic way that I can give the user a simple input to
select a directory
The basic way to do this is:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
The rest maybe not that obvious (you have to ask for the chosen file to get the directory):
int returnVal = chooser.showDialog(chooser, "Directory to save");
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile());
}
In the code given below, taken from the JAVA API page for class JFileChooser:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
It is supposed to open a popup window to prompt for a file in the user's directory. May I know how we should initialize the 'parent' variable, or what values to assign to it so that this dialog window points to the user's directory?
Just assign it a null value. If this is in your applet class or JFrame class you can also put this.
To get the user's home directory you should use a system property:
System.out.println("User Home Path: "+System.getProperty("user.home"));
File parent = new File(System.getProperty("user.home")); // User home directory
In your case however the parent variable is of Component class. That means that you are supposed to pass it a JFrame or other AWT/Swing component which is the dialog's parent. Passing null here will create a dialog that is not related to any other GUI component.
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";
}