• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with file chooser

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any comments ??
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
kumar Reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic