Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jfilechooser dropdown option

Posted on 2008-10-27
2
Medium Priority
?
360 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 1000 total points
ID: 22819462
chooser.setAcceptAllFileFilterUsed(false);

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

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Suggested Courses

715 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