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)
Related
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.
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 want show all files of server using jfilechooser and choose any file then download that.
But i don't know that, is it possible? Please give me some suggestion?
It's not easy but I think it's possible. You need to set a custom FileSystemView JFileChooser.setFileSystemView. Try to use google search (filesystemview ftp). Probably you can find implementation of FileSystemView which supports FTP.
You can make use of FileNameExtensionFilter. Using it you can add extensions for selection.
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg", "gif");
chooser.setFileFilter(filter);
i have this issue working but i would like to know if there is a better way of adding the file extension?
what i am doing right now is:
String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";
im adding the extension hard coded. and then saving to it.
just wondering if there is a more robust/logical manner this can be implemented?
thank you for your time.
EDIT: i ask this as i would like my app to be portable across platforms. so adding .html manually i may make this a windows only solution.
EDIT: i think ive surfed enough to know that .html hard coded is safe as i havent found any documentation that says dont take this approach (not completely sure).
ISSUE: also if i want to save the file in another format, text, for example how do i detect that the user selected which format?
FileNameExtensionFilter can add filters to the dialog but how do i get the return value for file type selected?
EDIT: i have studied this but still unclear how to retrive user selected file type.
EDIT: this is a rephrase of my issue:
alt text http://img98.imageshack.us/img98/4904/savef.jpg my question is how can i retrieve/find out which one of the two filters the user has selected as the save format. HTML or JPEG? how do i retrieve this info from JFileChooser? thank you.
EDIT: found something out: it has something to do with JFileChooser.getFileFilter()
your help still welcome.
EDIT: getFileFilter() and FileNameExtensionFilter comparasion solved this issue.
Here is the code snippet that solves the issue:
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");
chooser.setFileFilter(filter);
int option = chooser.showSaveDialog(ChatGUI.this);
if (option == JFileChooser.APPROVE_OPTION) {
// Set up document to be parsed as HTML
StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
try {
System.out.println(chooser.getFileFilter());
if (chooser.getFileFilter() == filter)
System.out.println("ha ha");
}
}
You're probably looking for this:
The trick consists in casting the returned FileFilter to FileNameExtensionFilter and then apply getExtensions().
JFileChooser fileChooser = new JFileChooser("");
// Prevent user to use the default All Files option
fileChooser.setAcceptAllFileFilterUsed(false);
[...]
// Get the FileFilter
FileFilter ff = fileChooser.getFileFilter();
// Cast the FileFilter to FileNameExtensionFilter
FileNameExtensionFilter extFilter = (FileNameExtensionFilter)ff;
// Get the Extension
String ext = extFilter.getExtensions()[0];
Or, for making it compact:
ext = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0];
I don't understand what it is you're trying to do. Are you trying to save the selected file in some other format than it already is of? The path of the selected file will contain the file extension, so you don't need to add it manually. The following, for example, will print "/Users/banang/Documents/anything.html" to the screen if the file anything.html is selected.
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
System.err.println(chooser.getSelectedFile().getCanonicalPath());
Please try to clarify your question a bit.
So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.
You choose a file like this:
File fileToRead = JFileChooser.getSelectedFile();
Then you read and change the content and write it back to the same location with a different name:
File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );