• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

jfilechooser dropdown option

hi expert

im trying to create Jfilechooser that only display 2 type of file, pdf and txt

i want to delete the "All files" option from the drop down

so there is only 2 option in the dropdown: pdf or txt

how to do this things

  • 2
1 Solution
Kevin CrossChief Technology OfficerCommented:
You will need to implement FileFilter on the JFileChooser object.  Here is an example.  Try modifying it to fit your needs and then post back if you have any issues:
JFileChooser chooser = new JFileChooser();
          chooser.addChoosableFileFilter(new FileFilter() {
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                String extension = getExtension(f);
                if (extension != null) {
                    return ((extension=="rtf")?true:false);
                return false;
            public String getDescription() {
                return "*.RTF";
            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;

Open in new window

Kevin CrossChief Technology OfficerCommented:

This line disallows the "All Files" selection.  The FileFilter handles the inclusive extensions.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now