Solved

jfilechooser dropdown option

Posted on 2008-10-27
2
318 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 138
Java asynchronous logging 4 50
object oriented programming comparison 5 55
How to convert String matching to regex in java 4 21
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

896 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

12 Experts available now in Live!

Get 1:1 Help Now