Solved

jfilechooser dropdown option

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 162
Arrays.asList  VS  ArrayList 4 59
hibernate example for saving data 19 39
by zero exception 10 41
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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