Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JFileChooser validating file

Posted on 2007-11-29
4
Medium Priority
?
2,344 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:boardtc
  • 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:boardtc
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 150 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:boardtc
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

916 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