Active FileFilter in JFileChooser

Ok, here is my problem:
I successfully installed 2 TextFilters to my JFileChooser. But unfortunately when i open the JFileChooser Window only the last added FileFilter is active...But i want to start this window with the common file filter (*.*). So how can i set the active FileFilter from beginning?
here is my Code:
                        fileChooser = new JFileChooser();
               FileView fv = new ImageFileView();
               fileChooser.setFileView(fv);
               fileChooser.setFileFilter(new MyFileFilter(".pdf","Adobe Acrobat files (*.pdf)"));
               fileChooser.setFileFilter(new MyFileFilter(".rtf","Rich Text Format files (*.rtf)"));
               fileChooser.showOpenDialog(this);

i know how to add fileFilter to my JFileChooser...and
i read the FM!I figured this out in 2 minutes. Was this really so hard?
private static void JFileChooserTest() {
          JFileChooser chooser = new JFileChooser();
          FileFilter o_filter = chooser.getFileFilter();
          FileExtensionFilter filter = new FileExtensionFilter.HTMLFilter();
          FileExtensionFilter filter2 = new FileExtensionFilter.RTFFilter();
          FileExtensionFilter filter3 = new FileExtensionFilter.TextFilter();
          chooser.addChoosableFileFilter(filter);
          chooser.addChoosableFileFilter(filter2);
          chooser.addChoosableFileFilter(filter3);
          chooser.setFileFilter(o_filter);
          chooser.showOpenDialog(null);
     }

Similar Messages

  • Filefilter for jfilechooser --Confused-

    JFileChooser JFC=new JFileChooser();
    JFC.setFileFilter(new ExeFileFilter());
    class ExeFileFilter implements FileFilter
    {          public boolean accept(File TheFile)
              if(TheFile.getName().endsWith("exe"))
              return true;
         return false;
    }the above code doesnt work, tried several possibilities
    can anyone help me??

    what i want is that the JFilechooser show only exe
    file..
    helpdid you read the examples in JFilechooser Tutorial?
    all you have to do is modify to it.
    here's the code:
    ImageFilter Class
    import java.io.File;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class ImageFilter extends FileFilter {
        //Accept all directories and all gif, jpg, tiff, or png files.
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            String extension = Utils.getExtension(f);
            if (extension != null) {
                if (extension.equals(Utils.tiff) ||
                    extension.equals(Utils.tif) ||
                    extension.equals(Utils.gif) ||
                    extension.equals(Utils.jpeg) ||
                    extension.equals(Utils.jpg) ||
                    extension.equals(Utils.png)) {
                        return true;
                } else {
                    return false;
            return false;
        //The description of this filter
        public String getDescription() {
            return "Just Images";
    }Utils Class
    import java.io.File;
    import javax.swing.ImageIcon;
    /* Utils.java is used by FileChooserDemo2.java. */
    public class Utils {
        public final static String jpeg = "jpeg";
        public final static String jpg = "jpg";
        public final static String gif = "gif";
        public final static String tiff = "tiff";
        public final static String tif = "tif";
        public final static String png = "png";
         * Get the extension of a file.
        public static String getExtension(File f) {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');
            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            return ext;
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = Utils.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
    }

  • File Filtering in FileDialog on Windows

    We have a Java application that we are deploying primarily on Windows platforms. We use FileDialog to perform a "file save as" function. We would like to filter the list of files shown to match a file extension. Even better, we would like to populate the File Types dropdown on the standard Windows dialog.
    It is well known that FileNameFilter does not work on the Windows platform, so that is not a viable solution.
    We have tried JFileChooser, but performance was intolerable when a directory had many files (many = thousands). (Sure, we can tell the user not do have so many files in directories, but we can't control what the user does so this only leads to bug reports that our application is hung/broken while JFileChooser takes minutes to fill the dialog.)
    This leads me to think that we need a JNI solution for use on Windows (we can use FileNameFilter on other platforms). Are there any low cost implementations available? I'd appreciate any suggestions.
    Thank you.
    Guy

    I have used FileFilter with JFileChooser. Performance was not adequate, as I described in my post. I need a solution which a) works and b) has adequate performance.

  • Multiple FileFilter in a JFileChooser

    I wanna have a save file dialog with a list of choosableFileFilter that the user can select. Here is my code :
    import java.io.File;
    import javax.swing.filechooser.FileFilter;
    public class ImageFilter extends FileFilter {
        private static String description;
        private static String extension;
        private static String loadOrSave;
        private final static String jpeg = "jpeg";
        private final static String jpg = "jpg";
        private final static String gif = "gif";
        private final static String tiff = "tiff";
        private final static String tif = "tif";
        private final static String png = "png";
        public ImageFilter(String ext,String desc,String type) {
            super();
            extension = ext;
            description = desc;
            loadOrSave = type;
        public boolean accept(File f) {
            if (loadOrSave.equals("load")) { // "load" ==> only accept files with the final extension above
                if (f.isDirectory()) {
                    return true;
                String ext = getExtension(f);
                if (ext != null) {
                    if (ext.equals(tiff) || ext.equals(tif) || ext.equals(gif) ||
                        ext.equals(jpeg) || ext.equals(jpg) || ext.equals(png)) {
                        return true;
                    else return false;
                return false;
            else { // "save" ==> only accept files with extension "extension"
                if (f.isDirectory()) {
                    return true;
                String ext = getExtension(f);
                if (ext != null) {
                    if (ext.equals(extension)) return true;
                    else return false;
                return false;
        // Get the extension of a file.
        public static String getExtension(File f) {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');
            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            return ext;
        //The description of the Image filter
        public String getDescription() {
            if (loadOrSave.equals("load")) return "Images (*.jpg,*.jpeg,*.gif,*.png,*.tiff,*.tif)";
            else return description;
        public static String getSaveExtension() {
            if (loadOrSave.equals("save")) return extension;
            else return null;
    }I then use my class in the following code fragment :
    JFileChooser fc = new JFileChooser(currentdir);
            ImageFilter bmp = new ImageFilter("bmp","Images BMP (*.bmp)","save");
            ImageFilter jpeg = new ImageFilter("jpeg","Images JPEG (*.jpeg)","save");
            ImageFilter jpg = new ImageFilter("jpg","Images JPG (*.jpg)","save");
            ImageFilter png = new ImageFilter("png","Images PNG (*.png)","save");
            ImageFilter wbmp = new ImageFilter("wbmp","Images WBMP (*.wbmp)","save");
            fc.addChoosableFileFilter(bmp);
            fc.addChoosableFileFilter(jpeg);
            fc.addChoosableFileFilter(jpg);
            fc.addChoosableFileFilter(png);
            fc.addChoosableFileFilter(png);
            fc.setAcceptAllFileFilterUsed(false);
            int returnVal = fc.showSaveDialog(fc);The problem is that the choosable FileFilters in the save dialog have the same description : Images WBMP (*.wbmp). I don't understand why. I created different ImageFilters and then add them one by one. So where's the problem ?
    Thx in advance.

    Damned !
    I'm always asking myself if I have to use the static modifier for a class variable.
    I don't really understand the concept of static variables...
    I think static variables belongs to the class and not to the instances of this class.. Am I right ?

  • Minor JFileChooser/FileFilter query.

    Ok.
    I've made my own FileFilter. I've integrated this FileFilter with the JFileChooser (using fc.setFileFilter).
    Now, uhm.. so now my JFileChooser's file type drop-down list has 2 entries: "All files" and "MLP files".
    You see now, "MLP files" shows only those files with the extension ".mlp". Is there a way I can show the directories also with the "MLP files" FileFilter?
    Also, is there a way to completely remove the "All files" filefilter, which is the default file filter.

    You can display files and directories using the public static final int FILES_AND_DIRECTORIES
    see this link
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html#FILES_AND_DIRECTORIES
    but the functionality will display all directories, but only the file extensions that you set in the filter will be shown in the directory......
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html
    http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

  • Using jFileChooser and FileFilter

    I am using a jFileChooser and want to add a file filter to only show .jpg files. I tried using...
    FileFilter filter = new FileFilter();
    filter.addExtension("jpg");
    filter.setDescription("JPG Images");
    fileChooser.setFileFilter(filter);
    fileChooser.showSaveDialog(jPanel1)I get an error saying FileFilter is an abstract. I was wondering how I can successfully filter a jFileChooser. Also, what imports libraries do I need to perform the task. I have looked at sample code and it seems they do their own classes, I want to know how to handle this task using the libraries java provides.

    Here's another way to do it, using an anon. inner
    class:
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new FileFilter() {
    public String getDescription() { return "Image
    files"; }
    public boolean accept(File f) {
    if(f.isDirectory()) return true;
    if(f.getName().endsWith(".jpg")) return true;
    if(f.getName().endsWith(".png")) return true;
    return false;
    /code]
    I find myself using a lot of anon. inner classes in
    Swing.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • FileFilter  doesnot work in JFileChooser

    Hi all
    I am creating my own file filter so that jfilechooser will show only specified files. But I am not able to see any files other than directories.
    Here is my code:
    class MyFilter extends javax.swing.filechooser.FileFilter{
         public boolean accept(File f) {
              if (f.isDirectory()) {
                   return true;
                   String ext = null;
                   String s = f.getName();
                   int i = s.lastIndexOf('.');
                   if (i > 0 && i < s.length() - 1) {
                        ext = s.substring(i+1).toLowerCase();
                     String extension = ".rl"; //accepting only '.rl'files
                 if(ext !=null){
                         if (extension.equals(ext)){
                                return true;
                     } else {
                            return false;
                 }else
                   return true;
         }//end of fucntion
         //The description of this filter
         public String getDescription() {
              return "*.rl";
         } //end of function
    }//end of classhere is how I create JFileChooser:
    chooser=new JFileChooser();
    chooser.setMultiSelectionEnabled(false);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setControlButtonsAreShown(false);          
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.addChoosableFileFilter(new MyFilter());
    chooser.setAcceptAllFileFilterUsed(false);Pls suggst me about how to resolve this.
    Regards

    Its working now. I cahnged my sode as:
    public boolean accept(File f) {
              if (f.isDirectory()) {
                   return true;
              String ext = null;
              String s = f.getName();
              if(s.endsWith(".rl"))
                   return true;
              else
                   return false;
    }//end of f unction

  • JFileChooser - activating buttons with the enter key

    Hi All,
    I am trying to figure out how to activate the cancel button in the file chooser by tabbing to it, and than pressing the enter key. I know how to do this in a regular dialog, but I can't seem to get the access I need to the JFileChooser to get it to work there.
    I would appreciate any help.
    Thanks!
    robinste

    The Buttons in the File Chooser by default dont use the Enter Key.
    You must make the Enter key work if you are to use it
    after you tab to get the focus on to it.
    For this to be done you will need to extend the FileChooser
    and change the key listener for the cancel button .......
    Hope t was of some help ............

  • How can I get an extension from a file...in JFileChooser

    I have got a problem here. Here I have a JFileChooser, I want to add in
    a FileFilter, and user could only see some type of file. But as long as I
    try to get the extension....It wont compile, could any one teach me how
    to get the extension of a file in JFILECHOOSER???
    ============================================
    import java.io.*;
    import java.io.File.*;
    import javax.swing.filechooser.FileFilter;
    public class MainFrame extends JFrame implements ActionListener {
    public MainFrame() {.............}
    private boolean chooseFile()
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
    FileChoser filtering = new FileChoser("txt");
    fileChooser.setFileFilter(filtering);
    fileChooser.addChoosableFileFilter(filtering);
    int selection = fileChooser.showOpenDialog( this );
    if ( selection == JFileChooser.CANCEL_OPTION )
    return false;
    if (selection == -1) {
    return false;
    File tmpfile = fileChooser.getSelectedFile();
    filename = tmpfile.toString();
    setTitle( "Smiggin Holes 2010 - " + filename );
    return true;
    static class FileChoser extends javax.swing.filechooser.FileFilter
    String extension;
    String description;
    public FileChoser(String extension, String description){
    this.extension = extension;
    this.extension = description;
    // Accept all directories and all gif, jpg, or tiff files.
    public boolean accept(File f)
    if (f != null)
    if (f.isDirectory())
    return true;
    String ext = getExtension(f); //<<<<<<<<<<<THE PROBLEM IS HERE WHEN I TRY TO GET THE EXTENSION OF THE FILE. IT CANT COMPILE
    System.out.println("aasdfasdf"+ext);
    if ((ext!=null)&&(ext.equals(extension)))
    return true;
    return false;
    // The description of this filter
    public String getDescription() {
    return "Just Text Files";
    ======================================
    Could you tell me what can I do? To get the extension of the file selected by the user?
    And What do I have to change?
    Regards,

    Hi,
    can you post your compile error and the code of getExtension().
    Regards
    Ldinka

  • JFileChooser and Read Only Folders in XP

    The following question concerns a Java application, not a applet or web service.
    If you are browsing a folder with JFileChooser the button to create a new folder is disabled if the parent folder is read-only. Under XP folders often show up as read only and cannot be easily changed by the user. Even if Windows reports that the parent folder is read only, it does not block the creation of sub-folders in it. I'd like to always make the create folder button active regardless of whether the parent folder is read only or not; that more truely reflects the folder creation permissions anyway and mimics the behaviour of an MFC file browser. How would I do this?
    I've even tried to add a listener to the file chooser so that as the user browses to a new directory I automatically change the write permissions of folder, but short of a system command, there's no way to change the write permissions from JAVA so I don't consider this a neat solution.
    I'm surprised that I haven't found similar complaints on the net. If you use a MFC file browser the create folder button is active, but a Java file chooser has it disabled for the same folder.
    Please help.

    What would you say to a new FileChooser? I love java but I'm no fan of Sun's choosers. (See www.MartinRinehart.com, Examples, ColorChooser.)
    Email me if you're interested in doing one.

  • JFileChooser question regarding the order of the files in a folder

    I was hoping someone had a way around this issue I am having with the JFileChooser. I have a folder that has filenames in it like text1.txt, text2.txt, and so forth all the way up to text300.txt. When the filechooser shows these files it shows the order of them as:
    text1.txt
    text10.txt
    text11.txt
    text2.txt
    text20.txt
    text21.txt
    and so forth.
    How come it doesn't list them like they would be in a folder window like:
    text1.txt
    text2.txt
    text3.txt
    text150.txt
    text151.txt
    text299.txt
    text300.txt
    Actually I know why JFileChooser does it this way, it is because it sorts them as Strings without taking into account the number at the end of a similar string.
    Anyway, I was wondering if anyone had some ideas of how I could alter the JFileChooser to list the contents of a folder like this in the "right" order. Hopefully that made sense. :) Thanks.

    Thank u for ur reply.... actually the thing is mine's one of the pre-ordered iphones n it's reaching me by 2moro i think. so are the pre-ordered iphone's already activated? or m i in trouble if it's not activated out of the box (if i have to activate it myself). ?

  • Loading XML Document from JFileChooser

    My initial version of this app I hardcoded the XML filename. Now, I would like to load an xml file via a JFileChooser. The problem I'm having is that I create the DOM Document in the constructor because of dependencies on the JTree. Well, I don't call showDialog until the "open" action is called which is further into the program. By then it's too late. Here is the code. I extracted some unecessary code for reading purposes.
    public class DomGui extends JPanel implements DomGuiConstants, ActionListener
        private JScrollPane treeView;
        private String xmlFilename;
        private Document document;
        private JFileChooser fileChooser;
        private ExtensionFilter fileFilter;
        private static JFrame frame;
        private static JTree jtree;
        public DomGui() throws Exception
            frame = new JFrame(DOM_VIEWER);
            fileChooser = new JFileChooser();
            fileFilter = new ExtensionFilter(XML_EXTENSION, XML_DESCRIPTION);
            // Create the Document
            //xmlFilename = "W:/RoseModel/ImplementationView/seng/ewcs/clwc/Simulation/Utilities/Xml/DomViewer/TVschedule.xml";
            try
                CreateDomDocument createDomDocument = new CreateDomDocument(xmlFilename);
                document = createDomDocument.getDocument();
            catch (FileNotFoundException fnfe)
                System.out.println(fnfe);
                System.exit(1);
            // Create a Tree Model
            DomTreeModel model = new DomTreeModel(document);
            // Create a renderer
            DomTreeCellRenderer  treeCellRenderer = new DomTreeCellRenderer();
            // Create the JTree
            jtree = new JTree(model);
            // Create an Editor
            DomTreeCellEditor treeCellEditor = new DomTreeCellEditor(jtree);
            // Build left-side view
            // an empty tree and put it a JScrollPane so users can see
            // its contents as it gets large
            treeView = new JScrollPane(jtree);
            treeView.getViewport().add(jtree);
            treeView.setPreferredSize(new Dimension( leftWidth, windowHeight ));
            // Create a JSplitPane to hold the left side JTree
            // and the right side JEditorPane
            // Build split-pane view
            splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
                                                       treeView,
                                                       htmlView );
            splitPane.setContinuousLayout( true );
            splitPane.setDividerLocation( leftWidth );
            splitPane.setPreferredSize(new Dimension
                                                        ( windowWidth + 10, windowHeight+10 ));
            // Add GUI components
            this.setLayout(new BorderLayout());
            this.add("Center", splitPane );
        public static void makeFrame()
            int FRAME_WIDTH = 670;
            int FRAME_HEIGHT = 600;
            // Set up the tree, the views, and display it all
            try
                final DomGui guiPanel = new DomGui();
                frame.getContentPane().add("Center", guiPanel );
            catch (Throwable throwable)
                throwable.printStackTrace();
                System.exit(1);
        public void actionPerformed(ActionEvent actionEvent)
            String actionCommand = actionEvent.getActionCommand();
            System.out.println("Action: " + actionCommand);
            if (actionCommand.equalsIgnoreCase("Quit"))
                System.exit(0);
            else if (actionCommand.equalsIgnoreCase("Open"))
                showDialog("Open XML File",
                                 "Open",
                                 "Open the file",
                                 'o',
                                 null);
        // end actionPerformed
        public File showDialog (String dialogTitle,
                                            String approveButtonText,
                                            String approveButtonToolTip,
                                            char approveButtonMnemonic,
                                            File file)
            fileChooser.setDialogTitle(dialogTitle);
            fileChooser.setApproveButtonText(approveButtonText);
            fileChooser.setApproveButtonToolTipText(approveButtonToolTip);
            fileChooser.setApproveButtonMnemonic(approveButtonMnemonic);
            fileChooser.setFileSelectionMode(fileChooser.FILES_ONLY);
            fileChooser.rescanCurrentDirectory();
            fileChooser.setSelectedFile(file);
            fileChooser.addChoosableFileFilter(fileFilter);
            fileChooser.setFileFilter(fileFilter);
            int result = fileChooser.showDialog(this, null);
            System.out.println("Result is: " +result);
            if (result == fileChooser.APPROVE_OPTION)
                System.out.println("I'm Here 1");
                System.out.println("File selected: " + fileChooser.getSelectedFile());
                xmlFilename = fileChooser.getSelectedFile().toString();
                return fileChooser.getSelectedFile();
            else
                System.out.println("I'm Here 2");
                return null;
        // end showDialog
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    In your actionPerformed() method, shouldn't you do something with the File returned by showDialog()? For instance:
            else if (actionCommand.equalsIgnoreCase("Open"))
                xmlFilename = showDialog("Open XML File",
                                 "Open",
                                "Open the file",
                                'o',
                                 null).getName();
            }Because it seems that, for now at least, you're choosing a file and then just kinda throwing it away...

  • How to save a file in Jfilechooser with extension

    hi all
    i'm trying to save a file with a certain extension
    i have my class that extends FileFilter, and which write into the jfilechooser combo box with extensions. .jar
    how can i save a file with this extension?
    here is the code
    JFileChooser fileChooser = new JFileChooser();
              fileChooser.addChoosableFileFilter(new JarFilter());
              int returnVal = fileChooser.showDialog(new JFrame(), "Export");
              if (returnVal == JFileChooser.APPROVE_OPTION) {
                   File file = fileChooser.getSelectedFile();
                   try {
                        JarExporter exporter=new JarExporter(this.generateXMLString(file.getName().replaceAll(".jar", "")),
                                       this.editor.getLinkageCanvas().getSelectedWidgetIDs());
                        exporter.exportJarFile(file);
                   } catch (Exception exception) {
                        System.out.println("Export error.");
              }

    hi all
    i'm trying to save a file with a certain extension
    i have my class that extends FileFilter, and which write into the jfilechooser combo box with extensions. .jar
    how can i save a file with this extension?
    here is the code
    JFileChooser fileChooser = new JFileChooser();
              fileChooser.addChoosableFileFilter(new JarFilter());
              int returnVal = fileChooser.showDialog(new JFrame(), "Export");
              if (returnVal == JFileChooser.APPROVE_OPTION) {
                   File file = fileChooser.getSelectedFile();
                   try {
                        JarExporter exporter=new JarExporter(this.generateXMLString(file.getName().replaceAll(".jar", "")),
                                       this.editor.getLinkageCanvas().getSelectedWidgetIDs());
                        exporter.exportJarFile(file);
                   } catch (Exception exception) {
                        System.out.println("Export error.");
              }

  • JFileChooser - File save as

    Hi,
    I have a method in my application called "export" which exports the JPanel content into a file.
    I used the JFileChooser to let the users choose their desired location for saving the file.
    The problem is unless user explicitly types in the file format, it saves the file with no extension.
    How can I have formats like jpg, png in the File Type drop down menu.
    The code is on a different computer, so dont worry about syntax errors, I just write the important bits:
    JFileChooser jfc = new JFileChooser();
    int answer = jfc.showSaveDialog(this);
    if(answer==jfc.APPROVE_OPTION) {
       try {
          File file = jfc.getSelectedFile();
          ImageIO.write(Graph, "png", file); //Graph is what that needs to be exported 
       } catch () {}any help, highly appreciated, thanks

    TheParthian wrote:
    still got the problem,
    I implemented the FileFilter and its two methods, but what I get in the menu is the string i used in the getDescription method.Yeah, that's what the description is meant for.
    what if i want to include more than one extension?Add more filters.
    Also, even if I choose that as the file type, it still saves it with no extension. Do I need to get the string from the menu and add it
    manually to the end of file name?Yes, of course. (That is, you get the selected filter, determine the appropriate extension and add it, if the user did not explicitly specify an extension.)
    BTW - why did you start a new thread? You could have continued in your old one http://forums.sun.com/thread.jspa?threadID=5334819&messageID=10440802#10440802.

  • Problem in JFileChooser

    I have got a problem here. Here I have a JFileChooser, I want to add in
    a FileFilter, and user could only see some type of file. But as long as I
    try to get the extension....It wont compile, could any one teach me how
    to get the extension of a file in JFILECHOOSER???
    ============================================
    import java.io.*;
    import java.io.File.*;
    import javax.swing.filechooser.FileFilter;
    public class MainFrame extends JFrame implements ActionListener {
    public MainFrame() {.............}
    private boolean chooseFile()
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
    FileChoser filtering = new FileChoser("txt");
    fileChooser.setFileFilter(filtering);
    fileChooser.addChoosableFileFilter(filtering);
    int selection = fileChooser.showOpenDialog( this );
    if ( selection == JFileChooser.CANCEL_OPTION )
    return false;
    if (selection == -1) {
    return false;
    File tmpfile = fileChooser.getSelectedFile();
    filename = tmpfile.toString();
    setTitle( "Smiggin Holes 2010 - " + filename );
    return true;
    static class FileChoser extends javax.swing.filechooser.FileFilter
    String extension;
    String description;
    public FileChoser(String extension, String description){
    this.extension = extension;
    this.extension = description;
    // Accept all directories and all gif, jpg, or tiff files.
    public boolean accept(File f)
    if (f != null)
    if (f.isDirectory())
    return true;
    String ext = getExtension(f); //<<<<<<<<<<<THE PROBLEM IS HERE WHEN I TRY TO GET THE EXTENSION OF THE FILE. IT CANT COMPILE
    System.out.println("aasdfasdf"+ext);
    if ((ext!=null)&&(ext.equals(extension)))
    return true;
    return false;
    // The description of this filter
    public String getDescription() {
    return "Just Text Files";
    ======================================
    Could you tell me what can I do? To get the extension of the file selected by the user?
    And What do I have to change?
    Regards,

    Hi !
    in the FileChoser class, the public boolean accept(File f) method can be written like this :
    public boolean accept(File f) {
    if (f.isDirectory || f.getName().endsWith(".txt")) { // you can add other extensions
    return true;
    else {
    return false;

Maybe you are looking for

  • Problem with C4 and C5 On-Demand

    We've been using the YouView box for about three weeks, but have had some disappointing results.  On the positive side, picture quality is very good through HDMI and the pause live TV and recording all work fine. The main problem is with On-demand, m

  • Zany image for full screen preview

    sometimes I get  this kind of thing when View>full screen preview (reduced to fit): Hard to evaluate.  Need to restart computer but i dont want to. mac OSX.7.4 bridge 6 up to date

  • Any help appreicated on trial download of illustrator cs6

    I SUCCESSFULLY DOWNLOADED ILLUSTRATOR BUT AM UNABLE TO LAUNCH. MESSAGE STATES I NEED A JAVA RUNTIME BUT THAT I AM NOT CONNECTED TO THE INTERNET. I AM CONNECTED. I AM USING MAC OS 7.3. I WOULD LIKE TO TRY OUT THIS PRODUCT, BUT CANT GET IN! PLEASE HELP

  • Setting up a mirror

    During the course of (the excellent) LCA I spoke to a number of people about setting up an official mirror here in New Zealand (the one we currently have is somewhat 'under the radar'). I am now getting questions about how much bandwidth, disk space

  • Can we please go back to the old Podcast software

    The new iTunes interface is overly complex and needs a major overhaul. Particularly how one interfaces with one's podcast subscriptions.  There are so many options all over the place (in the iPhone section, in the Mac library of podcasts) and they do