Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:38:17+00:00 2026-06-16T05:38:17+00:00

I’m new to Java so please bear with me. In my program, I want

  • 0

I’m new to Java so please bear with me.

In my program, I want to give the user the ability to save a file to the directory of their choice. After doing a little research, I found this nifty class called JFileChooser. What I want to do is allow the user to go to their desired directory through the JFileChooser GUI, type a name for their file, and allow them to save their file to the desired directory. I tried looking online for a solution to how to do this but almost everywhere I read, the final answer was “Now you have to make your program save the file” which I have no idea how to do. Could someone provide some well commented dummy code that would do the above description? Also, does anyone know whether JFileChooser provides a “New Folder” option?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:38:19+00:00Added an answer on June 16, 2026 at 5:38 am

    It’s not trivial to put all of the pieces together. You need a FileFilter to save to particular extensions.

    Here’s an example from one of my Swing applications.

    protected static final String EXTENSION = ".png";
    
    protected static final String FORMAT_NAME = "png";
    
    protected static final LayoutFileFilter SAVE_AS_IMAGE = 
            new LayoutFileFilter("PNG Image Format", EXTENSION, true);
    
    protected int chooseSaveFile(BufferedImage image) {
        JFileChooser fileChooser = new JFileChooser();
        ExtensionFileFilter pFilter = new ExtensionFileFilter(SAVE_AS_IMAGE);
        fileChooser.setFileFilter(pFilter);
        int status = fileChooser.showSaveDialog(frame.getFrame());
    
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
    
            try {
                String fileName = selectedFile.getCanonicalPath();
                if (!fileName.endsWith(EXTENSION)) {
                    selectedFile = new File(fileName + EXTENSION);
                }
                ImageIO.write(image, FORMAT_NAME, selectedFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        return status;
    }
    

    You’ll notice that there’s code in the method to ensure that the EXTENSION is appended on the end of the file name, unless the EXTENSION already exists. This is standard Windows behavior that is missing in the Java equivalent.

    In order to save a particular extension, you’ll need a FileFilter.

    import java.io.File;
    
    import javax.swing.filechooser.FileFilter;
    
    public class ExtensionFileFilter extends FileFilter {
    
        protected LayoutFileFilter filter;
    
        protected String description;
        protected String[] extensions;
    
        public ExtensionFileFilter(LayoutFileFilter filter) {
            this(filter.getDescription(), filter.getExtension());
            this.filter = filter;
        }
    
        public ExtensionFileFilter(String description, String extension) {
            this(description, new String[] {extension});
        }
    
        public ExtensionFileFilter(String description, String[] extensions) {
            if ((description == null) || (description.equals(""))) {
                this.description = extensions[0] + " {" + extensions.length + "}";
            } else {
                this.description = description;
            }
            this.extensions = (String[]) extensions.clone();
            toLower(this.extensions);
        }
    
        private void toLower(String[] extensions) {
            for (int i = 0, n = extensions.length; i < n; i++) {
                extensions[i].toLowerCase();
            }
        }
    
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else {
                String path = file.getAbsolutePath().toLowerCase();
                for (int i = 0, n = extensions.length; i < n; i++) {
                    String extension = extensions[i];
                    if (path.endsWith(extension)) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        @Override
        public String getDescription() {
            return description;
        }
    
        public LayoutFileFilter getLayoutFileFilter() {
            return filter;
        }
    
    }
    

    And finally, the LayoutFileFilter.

    public class LayoutFileFilter {
    
        boolean isDefault;
    
        String description;
        String extension;
    
        public LayoutFileFilter() {
    
        }
    
        public LayoutFileFilter(String description, String extension,
                boolean isDefault) {
            this.description = description;
            this.extension = extension;
            this.isDefault = isDefault;
        }
    
        public boolean isDefault() {
            return isDefault;
        }
    
        public void setDefault(boolean isDefault) {
            this.isDefault = isDefault;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getExtension() {
            return extension;
        }
    
        public void setExtension(String extension) {
            this.extension = extension;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have just tried to save a simple *.rtf file with some websites and
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm interested in microtypography issues on the web. I want a tool to fix:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.