?
Solved

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

Posted on 2005-02-25
7
Medium Priority
?
1,615 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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