Solved

JFileChooser validating file

Posted on 2007-11-29
4
2,341 Views
Last Modified: 2008-02-01
Using a JFileChooser with, eg,  *.txt, only in the files of type dropdown. the suier can then type *.ini in the File name text box and then open any ini file. See code sample below.

How can I only allow a certain file (*.txt in this example) to be opened, ie. this dialog not close unless a .txt file selected or the dialog is cancelled?

Thanks, Tom.
JFileChooser fileChooser = new JFileChooser(); 
			int returnValue = JFileChooser.CANCEL_OPTION;
			fileChooser.setDialogTitle("Open");
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setFileFilter(new FileFilter() {
				public boolean accept(File file) {
					String fileName = file.getName().toUpperCase();
					if (fileName.endsWith("*.txt")) {
						return true;
					}
					return false;
				}
	
				public String getDescription() {
					return "Text file (*.txt)";
				}
			});
				
			returnValue = fileChooser.showOpenDialog(parent);

Open in new window

0
Comment
Question by:tomcorcoran
[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
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20373116
you could check the file returned and re-pop it up if not correct type.
0
 

Author Comment

by:tomcorcoran
ID: 20373604
of course one can do that. I am looking for a solution where the file selected can be validated prior to the file chooser being closed...
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 50 total points
ID: 20373876
Then you have to open the source and write your own sub-version.
The source can be found as src.zip in the JDK distibution.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20374033
Ok. I am used to the Delphi community where lots of source is freely available, I have been surprised with my experiences in java so far that though free everyone keeps reinventing the wheel rather than using well oiled code...but maybe I have not been given the keys to the community yet, certainly delphi's is not exclusive....anyway....for such an obvious problem i'm amazed there is not a ready made solution....
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

623 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