Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to force an extension, for example *.xml, using a JFileChooser object

Posted on 2005-02-25
7
Medium Priority
?
1,650 Views
Last Modified: 2013-11-19
Hello

I'd like to have an example which show force an extension by default in the TextArea File name and Using the JFileChooser object.

I have created an object like this : JFileChooser jfc = new JFileChooser();
But i didn't find a method which enable me to force an extension, for example *.xml.

Thanks
Regards

AC
0
Comment
Question by:pofpof98
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13402488
Use a FileFilter
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13402490
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13402493
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13402502
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 13402506
In your case:

JFileChooser fileChooser = new JFileChooser(new File(filename));
    fileChooser.addChoosableFileFilter(new MyFilter());
   
    // Open file dialog.
    fileChooser.showOpenDialog(frame);
    openFile(fileChooser.getSelectedFile());
   
    class MyFilter extends javax.swing.filechooser.FileFilter {
        public boolean accept(File file) {
            if (file.isDirectory()) return true;  // <<<<<<<<< add this to be able to browse to directories
            String filename = file.getName();
            return filename.toUpperCase().endsWith(".XML");
        }
        public String getDescription() {
            return "XML files (*.xml)";
        }
    }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13402522
>> JFileChooser fileChooser = new JFileChooser(new File(filename));  // to have a default selected file
but can of course simply be: JFileChooser fileChooser = new JFileChooser();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13404458
Thanks for accepting
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

580 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