remove allfile option in jFilechosser java - java

i'm using swings jfilechooser in program,i want it to filter files with .txt extension,and it is showing allfiles option also in window,so i want to remove allfiles option,how can i do it plz help me
this is my code:
fc1 = new JFileChooser();
fc1.setMultiSelectionEnabled(true); // Allow for multiple selections
fc1.setCurrentDirectory(new File("C:\\"));
fc1.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".fls")
|| f.isDirectory();
}
public String getDescription() {
// TODO Auto-generated method stub
return "*.fls";
}
});
Thanks in adavance
mukta

Well, just by reading the javadoc, I can see that there's a method called setAcceptAllFileFilterUsed(boolean). Did you try that? It sounds like it's doing what you want.

Related

How to select all files in a FileDialog?

I want to select all files to exclude them from being displayed in my FileDialog.
FileDialog fileDialog = new FileDialog(this, "Some Title", FileDialog.LOAD);
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
if(name.endsWith(".*")) {
return false;
}else {
return true;
}
}
});
fileDialog.setVisible(true);
In my code you can see, that I am trying to to that with the String ".*", to select all files. This doesn't work however and I don't know why.
I only want to show directories.
Thanks for your help!
You can use a JFileChooser, using a FileFilter to check the File object to see if it is a directory
#Override
public boolean accept( File file ) {
return file.isDirectory();
}
A FileDialog's FileFilter should work similarly. Also note the API for the FileDialog's setFileFilter method:
"Filename filters do not function in Sun's reference implementation for Microsoft Windows."
As previously stated, I found the answer to this question with the help of #JigarJoshi.
This is the working code to show ONLY Directories on a AWT FileDialog:
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return dir.isFile();
}
});
Please note that using FileDialog over JFileChooser is only recommendet, if you are on a non-Windows system. However on Mac and Linux, you should prefer to use FileDialog since it looks more native.
Thank you very much for your input!

JFileChooser won't allow selection of directories

I want to use a JFileChooser in my program in order to choose a directory and process it. However, no matter what FileFilter I use for the file chooser, the Open button is locked when a directory is selected. Below is the code of my FileFilter.
this.fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
#Override
public String getDescription()
{
return "Directories";
}
#Override
public boolean accept(File f)
{
return f.isDirectory();
}
});
Have you tried setting the file selection mode? The default is JFilesChooser.FILES_ONLY, which means your custom FileFilter is effectively ignored even though you return true for directories.
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

How to fix bug of JFileChooser or avoid the bug preventing JFC to show shortcut (lnk) folders

Using NetBeans(java) I created a JDialog with a JFileChooser and when I try to open a lnk folder using the ComboBox (in other ways works fine) an exception is thrown and the folder is not opened.
I found out that is a bug:
I tried some workarounds but they didn't work.
Can you help me?
Alternatively, can you suggest me a "trick"?
I thought about prevent the JFileChooser(or at least the ComboBox) to show lnk folders but I don't know how to do it.
You can either prevent the symlinks to be displayed by using a FileFilter and the FileUtils from Apache Commons:
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
#Override
public String getDescription() {
return "All (without symlinks)";
}
#Override
public boolean accept(File f) {
try {
return !FileUtils.isSymlink(f);
} catch (IOException e) {
return true; // Maybe to be changed to false depending on
// your use case
}
}
});
An other alternative is to use a custom FileSystemView, as #Andrew Thompson suggested.
EDIT:
After reading your question more carefully, it looks like you more interested in filtering the Windows shortcuts. The same technique applies:
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
#Override
public String getDescription() {
return "All (without shortcuts)";
}
#Override
public boolean accept(File f) {
return !f.getName().endsWith(".lnk");
}
});

Issue using IOFileFilter

i have used Apache FileUtils and IOFileFilter to list all files under a folder recursively excluding .svn folders. Here is the code i tried
File selectedFolder = new File(path);\\path to folder to list
final IOFileFilter dirs = new IOFileFilter() {
#Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
#Override
public boolean accept(File file) {
// TODO Auto-generated method stub
if(file.getName().toLowerCase().equalsIgnoreCase(".svn")||file.getName().toLowerCase().contains(".svn"))
return false;
else return true;
}
};
filesList.addAll(FileUtils.listFiles(selectedFolder,dirs, TrueFileFilter.INSTANCE));
I am getting the error
java.lang.IllegalArgumentException: Parameter 'directory' is not a directory
at org.apache.commons.io.FileUtils.validateListFilesParameters(FileUtils.java:545)
at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:521)
Can anyone tell me where am going wrong. I feel there is something wrong with the filter used. I could not figure it out
Actually, FileFilterUtils contains a method called makeSVNAware that you could use. It returns a filter that ignores SVN directories. For example:
filesList.addAll(
FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE,
FileFilterUtils.makeSVNAware(null)));
Note that listFiles expects a file filter as its 2nd argument, and a dir filter as its 3rd. In your code they're the other way round. So if you wouldn't want to use makeSVNAware, your code would look something like this:
File selectedFolder = new File(path); // path to folder to list
final IOFileFilter dirs = new IOFileFilter() {
#Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
#Override
public boolean accept(File file) {
return (!file.getName().toLowerCase().equalsIgnoreCase(".svn"));
}
};
// 2nd argument: TRUE filter, returning all files
// 3rd argument: dirs filter, returning all directories except those named .svn
filesList.addAll(FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE, dirs));
It looks like you have split the functionality into two functions.
The second one should also check for isDirectory() and the first one should also check the name.

JFileChooser Help

I am trying to set the file filter for my JFileChooser. This is my code:
JFileChooser picker= new JFileChooser();
picker.setFileFilter(new FileNameExtensionFilter("txt"));
int pickerResult = picker.showOpenDialog(getParent());
if (pickerResult == JFileChooser.APPROVE_OPTION){
System.out.println("This works!");
}
if (pickerResult == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
When I run my program, the file chooser comes up, but it won't let me pick any .txt files. Instead, it says this in the console:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
How do i fix this?
You need to add at least one extension as a second paramter. From the API:
FileNameExtensionFilter(String description, String... extensions)
Parameters:
description - textual description for the filter, may be null
extensions - the accepted file name extensions
Also if you want an specific files extensions and navigate thru folders you can try this:
JFileChooser fc = new JFileChooser(path);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.addChoosableFileFilter(new FileFilter () {
#Override
public String getDescription() {
return "DAT Files";
}
#Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
return f.getName().endsWith(".dat");
}
});
fc.setAcceptAllFileFilterUsed(false);

Categories

Resources