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
Solved

JFileChooser validating file

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
web application structure 18 100
jsp login check 12 43
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 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