Can you preselect javax.swing.filechooser.FileFilter.getDescription()?

Let me try to illustrate the steps as simply as I possibly can.

1) You have already selected an image. From URL or via file.

2) You have stored the file name into a String that you can get via getFileName().

3) You have stored the file name extension into a String that you can get via getExt().

4) You will now generate a JFileChooser that will allow you to save the manipulated image file (you did some image stuff to it and you will save it as a newly-named image file)

/**

* Filter {@link javax.swing.JFileChooser}

* @param chooser {@link javax.swing.JFileChooser}

*/

privatevoid filterFileChooser(JFileChooser chooser){

chooser.setAcceptAllFileFilterUsed(false);

if (this.getFileFilterMap() ==null) this.generateLinkedHashMap();

Collection<javax.swing.filechooser.FileFilter> values = this.getFileFilterMap().values();

Iterator<javax.swing.filechooser.FileFilter> iter = values.iterator();

while (iter.hasNext()){

chooser.addChoosableFileFilter(iter.next());

}

for (int i = 0; i < chooser.getChoosableFileFilters().length; i++){

System.out.println(chooser.getChoosableFileFilters()[i].getDescription());

}

}

/**

* Generate {@link javax.swing.JFileChooser} for selecting location to save

*/

privatevoid generateNewFileChooser(){

JFileChooser chooser =new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

this.filterFileChooser(chooser);

int returnVal = chooser.showSaveDialog(ImageCropper.this);

if (returnVal == JFileChooser.APPROVE_OPTION){

// SAVE THE NEW CROPPED IMAGE AS A FILE

String fullExt = chooser.getFileFilter().getDescription();

String fileName = chooser.getSelectedFile().getPath() +

fullExt.substring(fullExt.lastIndexOf("*") + 1, fullExt.length());

ImageCropper.this.setFile(new File(fileName));

ImageCropper.this.processor.saveFile(ImageCropper.this.getFile());

}

}

5) Everything works just fine, except that what I would like to do is to be able topre-select the description value within the JFileChooser I generate to save the image file to match the same value within getExt().

Like for example, I have "blah1.jpg". I did stuff to "blah1.jpg". I will now save it as "blah2.jpg". I generate the JFileChooser, but what I want to see when I save the file type is "*.jpg" pre-selected for me (I still want my other choices, but I want to see "*.jpg" selected for me).

How is this possible?

Thanx

Phil

[3490 byte] By [ppowell777a] at [2007-11-27 0:45:44]
# 1

I'm not sure if I'm totally clear on what you want to do, but I gather you want to select the ".jpg" part of "image.jpg" in the JFileChooser's text field? If so, here's a really ugly and dirty way to get ahold of that text field

private void doIt() {

JFileChooser chooser = new JFileChooser();

JTextField textField = getChooserTextField(chooser);

if (textField != null) {

textField.setText("Hi there");

}

chooser.showSaveDialog(this);

}

private JTextField getChooserTextField(JFileChooser chooser) {

JTextField rtn = null;

List<Component> comps = getAllComponents(chooser);

for (Component comp : comps) {

try {

rtn = ((JTextField)comp);

break;

} catch (ClassCastException ignore) {}

}

return rtn;

}

private List<Component> getAllComponents(Container cont) {

List<Component> rtn = new ArrayList<Component>();

for (Component comp : cont.getComponents()) {

if (comp instanceof Container) {

rtn.addAll(getAllComponents((Container)comp));

}

rtn.add(comp);

}

return rtn;

}

With a reference to the JTextField, I assume you can select the relavent portions of the text yourself. This is definately a hack, and there may be a better way to do it that I'm not aware of.

BinaryDigita at 2007-7-11 23:11:14 > top of Java-index,Java Essentials,New To Java...
# 2

I'm sorry but that's not what I am looking to do. What I am looking to do is to produce a JFileChooser save dialog whereby the file types listed are all there, however, the file type of the original file you got before (e.g. you got "image.jpg"), will be the one defaulted to appear among the other file types (so you will see "*.jpg" displayed even though you can still choose "*.gif" or "*.png" or whatever you want).

ppowell777a at 2007-7-11 23:11:14 > top of Java-index,Java Essentials,New To Java...
# 3

Never mind, I got it.. and it was too easy to believe, all I needed was setFileFilter(javax.swing.filechooser.FileFilter) !

for (int i = 0; i < chooser.getChoosableFileFilters().length; i++) {

if (ext != null &&

!ext.equals("") &&

chooser.getChoosableFileFilters()[i].getDescription().toLowerCase().indexOf(ext.toLowerCase()) >= 0) {

chooser.setFileFilter(chooser.getChoosableFileFilters()[i]);

}

}

ppowell777a at 2007-7-11 23:11:14 > top of Java-index,Java Essentials,New To Java...