Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ExampleFileFilter/ ExtensionFileFilter

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i use my JFileChooser, i would like files of ONLY particular extensions to be viewed.
I have tried using both ExampleFileFilter and ExtensionFileFilter, but i am encountering problems when i try
to compile these...
my compiler just wont RECOGNISE ExampleFileFilter or ExtensionFileFilter.
anyone any ideas/suggestions?
thanks,
Marcus
 
aude tantot
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the way u call ur filefilter isn't good, here's an example of file filter taht selesct only xml file and the code to call it. Just try to adapt...

ExtensionFilter.java:
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
public class ExtensionFilter extends javax.swing.filechooser.FileFilter {
public ExtensionFilter(String desc, String[] extensions) {
this.desc = desc;
this.extensions = (String[])extensions.clone();
}
public boolean accept(File f) {
if (f.isDirectory() == true) {
return true;
}
String name = f.getName().toLowerCase();
int length = name.length();
for (int i = 0 ; i < extensions.length; i++) {
String ext = extensions[i];
if (name.endsWith(ext) &&
name.charAt(length - ext.length()) == '.') {
return true;
}
}

return false;
}
public String getDescription() {
return desc;
}
protected String desc;
protected String[] extensions;
}

and here's the code that use the file filter:
private void MFSaveAsMousePressed(java.awt.event.MouseEvent evt) {

JFileChooser jfc = new JFileChooser();
jfc.addChoosableFileFilter(new ExtensionFilter("Fichier XML", new String[] {".xml"}));
int resultat = jfc.showSaveDialog(this);

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!