JFileChooser File Name Restrictions

A

Alan

Hi All,

I want the JFileChooser dialog to be customized as follows.
The File Name text box in the JFileChooser should not accept
special characters, like :<>|*

I trapped the keyPressed event and consumed the event by saying
e.consume();
Even after doing that the character appears on the text box of
File Name.
When I click on save and check for the value the special
characters never appear since they are consumed.

But what I need is I don't want user to get the special characters
appear on the File Name text box when he/she types the same.
 
A

ak

I want the JFileChooser dialog to be customized as follows.
The File Name text box in the JFileChooser should not accept
special characters, like :<>|*

I trapped the keyPressed event and consumed the event by saying
e.consume();
e.consume() is really unclear thing, sometimes works it, sometimes not.
try to change ActionMap of JFileChooser's dialog.
 
L

Liz

ak said:
e.consume() is really unclear thing, sometimes works it, sometimes not.
try to change ActionMap of JFileChooser's dialog.
---------------
// Use a file filter, look in the sun tutorial for details.
fc.addChoosableFileFilter()
// override this function, if the filename has funny chars return false
// then the filenames won't appear in the fileChooser dialog box
public boolean accept(File f) {}
 
A

Alan

(e-mail address removed)

I got what you are saying,
But I am not able to do that!!! :(

Could you help me doing that.....
 
L

Liz

Alan said:
(e-mail address removed)

I got what you are saying,
But I am not able to do that!!! :(

Could you help me doing that.....

Here is a code segment and class that I have,
that I chopped up a bit to work for you. I
have not tested it.
----------------
String wd = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(wd);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.addChoosableFileFilter(
new FilenameFilter("no special characters"))
int rc = fc.showDialog(null, "Select Data File");
if (rc == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
...etc...
}
-------------------
import java.io.File;
import javax.swing.filechooser.FileFilter;

class FilenameFilter extends FileFilter
{
String description;

FilenameFilter(String desc)
{
description = desc;
}
//Accept all directories and all filenames that don't have special
characters
public boolean accept(File f)
{
String fname = f.getName();
if (fname != null)
{
// put some code here to see if the file/directory
// name contains special characters
// if everything is ok then
return true;
}
return false;
}
//The description of this filter
public String getDescription()
{
return description;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top