Solved

jfilechooser dropdown option

Posted on 2008-10-27
2
306 Views
Last Modified: 2012-06-21
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

thx..
0
Comment
Question by:iphone
  • 2
2 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22819459
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() {
 

            @Override

            public boolean accept(File f) {

                if (f.isDirectory()) {

                    return true;

                }

                

                String extension = getExtension(f);

                if (extension != null) {

                    return ((extension=="rtf")?true:false);

                }

                

                return false;

            }
 

            @Override

            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;

            }

          });

          chooser.setAcceptAllFileFilterUsed(false);

         

          chooser.showSaveDialog(null);

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22819462
chooser.setAcceptAllFileFilterUsed(false);

This line disallows the "All Files" selection.  The FileFilter handles the inclusive extensions.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now