permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes problem with file chooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with file chooser" Watch "problem with file chooser" New topic
Author

problem with file chooser

kumar Reddy
Greenhorn

Joined: Nov 24, 2004
Posts: 29
Hi all,

just got a small query regarding JFileChooser.

i want the user to restrict only to select .jar files . so i used

JFileChooser fileChooser = new JFileChooser("Select a jar file...");
JarFilter1 jFilter = new JarFilter1( "jar", "Only Jars" );
fileChooser.setFileFilter(jFilter);
FileFilter allfileFilter = fileChooser.getAcceptAllFileFilter();
fileChooser.removeChoosableFileFilter(allfileFilter);


The jarFilter is just like this



public class JarFilter1 extends FileFilter {

String m_desc = null;
String m_ext = null;

public JarFilter1(String m_ext, String m_desc) {
this.m_ext = m_ext;
this.m_desc = m_desc;

}

public boolean accept ( File f ) {
if ( f == null )
return false;

if ( f.isDirectory () )
return true;

return f.getName().toLowerCase().endsWith(m_ext);

}

public String getDescription () {
return m_desc;
}
}


int returnVal = fileChooser.showOpenDialog(this);

it shows only directories and jar files, that is fine..
but when i type sample.class or *.class in the filename text box of the dialog, *.class filter gets added to the combo box, so i can select the class file and press ok. and this file is returned back to me..

but i dont want the user to get out of the open dialog until he selects a .jar file...could you please let me know how could this be possible???

thank you
kumar Reddy
Greenhorn

Joined: Nov 24, 2004
Posts: 29
any comments ??
C. Alan
Ranch Hand

Joined: Dec 17, 2004
Posts: 30
I don't know how much help this will be, but let's see if I understand your problem clearly before I make any suggestions.

The FileChooser only shows .jar files and directories...that much I understand. What you are saying is that if a user types in another file with a different extension, then it will go ahead and open the file, even if it isn't a .jar file? And you want only jar files selected??

I've been tinkering around with your problem for a little bit, and I only see two ways that you could accomplish this. One would be to make the text field non-editable. The other option would be to check the input, and if the extension didn't match what you wanted, then empty the textfield. The only problem with this is that I don't think one can directly act on the textfield in a filechooser. I seem to remember having run across that problem for an assignment once.

SO I really only see a couple of options. One, that you create your own filechooser class. Two, check the extension of the file entered, and if it doesn't match .jar, create a new FileChooser. The final option is to maybe extend fileChooser.... I'm seeing if that can be done because I never considered doing it that way.

One other question...you mentioned that you didn't want the file chooser to close unless a .jar file was selected. What happens if your user wants to back out of the filechooser without selecting a file at all?


<a href="http://www.security-forums.com/forum/viewforum.php?f=48" target="_blank" rel="nofollow">Malware Removal</a> - Get your system running like new again.
kumar Reddy
Greenhorn

Joined: Nov 24, 2004
Posts: 29
Hi
Sorry for dealy in replying.

my problem in short.
i created a filter with .jar and added to filechooser.showopendialog().
now during exectuion, on start up, it shows the jar file and the folders..that i expected..thats fine until now...

now in the file name text box available, say if i type *.xml and press enter, now the xml files available in the current folder will be shown and "*.xml" will be added to Files of Type combo box. which is not intended.





I think the option of 1) not allowing the user to type in text box is not a good idea. if there are 100 files in a folder that is open, and he already know the name of that..he should be able to enter that.

2) idea of writing custom file chooser will work..will look into that..



I just dont want him to go out of the file dialog, if he selects other than a jar file and presses OK... However CANCEL can be used if he dont wanna have a selection.

thank you
 
 
subject: problem with file chooser
 
Similar Threads
Cant load files into array using listFiles. I think it's an eclipse problem
Only accept a file type in JFileChooser ?
JFileChooser!
Inner Class Question
JFileChooser