Solved

JFileChooser validating file

Posted on 2007-11-29
4
2,337 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 40
micro services spring boot application error 3 182
Spring Framework HTTPSession management 1 33
oracle 11g 23 73
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now