Solved

JFileChooser validating file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This video teaches viewers about errors in exception handling.
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.

705 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

17 Experts available now in Live!

Get 1:1 Help Now