FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Key files", "*.pem"),
);
The code is in the Java FX Controller. And my code uses a FXML file, where this code is executed upon clicking a button.
I am trying to accept only public key files from the Java FX input. Is this code snippet correct?
Is the extension filter right?
How can I check whether the file entered is a public key file? Is there a way to check or some tool?
How can I avoid InvalidKeySpecException?
Related
I'm trying to set a JFileChooser to only allow choosing a specific file type (pdf) via the showOpenDialog.
I've set a File Filter but I'm confused as to what action on the JFileChooser it has.
What I'm trying to achieve is:
Visually exclude other file types to prevent the user from choosing them from the list.
Actually prevent selection of other types or an invalid file. (i.e. have the getSelectedFile() to actually return a valid pdf file)
Here is my code:
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
fc.setDialogTitle("Load MSDS");
int op = fc.showOpenDialog(this);
if(op == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
lbl_msds_loaded.setForeground(Color.BLACK);
lbl_msds_loaded.setText(f.getName() + " (Size: " + utils.FileUtils.getFileSizeMegaBytes(f, 3) + ")");
}
I get this behavior:
Visually - The filtering works and the dialog does only show PDF Files, therefore I can only choose pdf files from the list.
But - I'm still able to manually select an invalid file, by typing in some name in the 'File name:' field and click open (or hit enter).
For example: if I write Untitled.png (which does exist in the currently opened directory) and open, I will get that png file loaded.
Or if write a file name that doesn't exist and click open, I will actually get a new file with that name loaded.
(By loaded I mean the file that getSelectedFile() will return).
Is there a way to not allow the dialog approve the open action if an invalid file is set (based on the filter ofcourse)?
Shouldn't this already be the case when using JFileChooser Dialogs with filters?
What exactly is the filter doing here? The documentation for JFileChooser does not explain any of these aspects.
I would really appreciate an explanation on how this works.
Also what is the difference between setFileFilter and addChoosableFileFilter? They give the exact same behavior.
Finally here's a few screenshots of the Dialog and the JFrame form I'm working on for some context:
https://ibb.co/bFVqVmt
https://ibb.co/5BcsXSW
https://ibb.co/2qq0qr9
https://ibb.co/jMXXXyN
https://ibb.co/g3kvtfd
https://ibb.co/2FshJpt
Thanks alot!
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 ;)
i'd like to know if there's a way of filtering the names of files to make them selectable in the dialog to select files, for instance all files that starts for "A" and are in txt format, i searched a bit and i found only tips topics about the extension with the Extension filter, that's fine but i'd like to select just some file in a format.
In JavaFX you can filter for particular file types by adding ExtensionFilters to the list of filters returned by getExtensionFilters, like so:
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
The JavaFX file chooser does not support filtering by file name, only by extension. This is because most platforms don't support this functionality natively in their file choosers.
Of course , you can get some idea from this example; in Java Swing( I'm not sure how in JavaFX) you can filter files by name or extension like :
FileChooser fileChooser = new FileChooser();
FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3");
fileChooser.setFileFilter(filter)`
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xls"));
you can use add or addAll depending upon how many filters you want to add.
My program has a folder called data - which stores text files- when i click save in my JAR application though nothing happens. How can i make it so i can access and save new files? Here is some of the code
public void saveAs() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Routine"); fileChooser.setInitialDirectory(new File("data/routines"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("txt", "*.txt"));
if (fileName != null) {
fileChooser.setInitialFileName(fileName);
}
save(fileChooser.showSaveDialog(stage));
}
I also did it with a absolute path and that did not work either
Without seeing your save(java.io.File) method, I can't say. FileChooser.showSaveDialog just shows the dialog and tells you what file they chose (if any). You then have to write it whether it's a JAR or txt.
Try this:
private void save(File where){
if(where==null){ //they canceled.
return;
}
try(OutputStream out = Files.newOutputStream(where.toPath())){
//Write to out.
}
}
You can write things into JAR files with the plain JarFile constructor, but it looks like you want plain text files. Although you may be able to update your application's JAR files while it's running, most programs don't do that because it's odd.
I am developing a java application for which I need only .xml files. Now I want to show only .xml files in JFileChooser whenever user wants to save a file or open a existing file.
Is this possible to show only .xml files?
You can use JFileChooser API to achieve your task.
For Open only .xml file
// create a filechooser;
JFileChooser chooser = new JFileChooser(cwd);
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
"xml files (*.xml)", "xml");
chooser.setDialogTitle("Open schedule file");
// set selected filter
chooser.setFileFilter(xmlfilter);
Also, go through javax.swing.filechooser.FileNameExtensionFilter.
If I remember right, you should use addChoosableFileFilter or setFileFilter method:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html#addChoosableFileFilter(javax.swing.filechooser.FileFilter)