Solved

jfilechooser dropdown option

Posted on 2008-10-27
2
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 total points
ID: 22819462
chooser.setAcceptAllFileFilterUsed(false);

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

617 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