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-
Related
I have to write a Java 8 Swing app and a part of the application generates an output file (Excel spreadsheet). So at some point in the UI, the user will have to:
Select a directory on their file system, where the Excel file will be written to; and then
Enter the name of the Excel file (if they specify ".xls" or ".xlsx" in the file name, the app will output/write the file in the respective XLS/XLSX output; if they omit the file extension the default will be XLSX)
I'm interested in what a decent UX solution here is, and how that maps to Swing controls and their layout.
I know I can use JFileChooser to choose a directory or a specific file, but I've never used it to select a directory and enter the name of a new (doesn't exist on the file system yet) file name + extension.
Any ideas as to what solution I can offer here that is functional, elegant and simple/easy to use & understand?
You can use Apache's FileNameUtils, or implement you own extension extraction with String manipulation using substring and indexOf... I'll give you an example of first case.
After using a default JFileChooser as suggested, you can just check for specified file name, as JFileChooser will return a File object (or null if nothing is selected, so first check for null values):
JFileChooser fileChooser = new JFileChooser();
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
String givenExtension = FilenameUtils.getExtension(selectedFile.getName());
boolean noExtension = "".equals(givenExtension);
boolean xlsx = givenExtension.toLowerCase().contains("xlsx");
boolean xls = givenExtension.toLowerCase().contains("xls");
String newFileName = selectedFile.getName();
if (noExtension) {
newFileName += ".xlsx";
} else if (!xlsx && !xls) {
throw new Exception("Invalid name");
}
}
Remove toLowerCase() if you don't want different cases to be accepted.
This should do what you want ;)
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());
}
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.
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.
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";
}