Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jfilechooser dropdown option

Posted on 2008-10-27
2
Medium Priority
?
365 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 60

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 60

Accepted Solution

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

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

783 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