I have one instance of a JFileChooser. If I select a file using the
JFileChooser then cancel the operation, the same file is shown as
selected the next time the JFileChooser is displayed. This happens
despite having reset the selected file(s) before the second display.
How can I redisplay the chooser without showing the previous
selection?
The code below does nothing but illustrate the situation. Press
"Create Index", select a file from the JFileChooser then press
"Cancel". Press "Create Index" again to see your file selected.
(I'm using java version 1.4.1)
package imageViewer;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
public class JFileChooserTest extends JFrame {
private JButton btnCreateIndex;
private JButton btnCancel;
private JFileChooser jpegFileChooser;
public JFileChooserTest(String title) {
super(title);
init();
}
public void init() {
setSize(400, 100);
getContentPane().add(getBtnCreateIndex(), BorderLayout.WEST);
getContentPane().add(getBtnCancel(), BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new JFileChooserTest("JFileChooserTest Clear Selection").show();
}
public JButton getBtnCreateIndex() {
if (btnCreateIndex == null) {
btnCreateIndex = new JButton("Create Index");
btnCreateIndex.addActionListener(new AbstractAction(){
public void actionPerformed(ActionEvent ae){
showJpegFileChooser();
}
}
);
}
return btnCreateIndex;
}
/**
* @return Returns the jpegFileChooser property.
*/
public JFileChooser getJpegFileChooser() {
if (jpegFileChooser == null) {
jpegFileChooser = new JFileChooser("");
jpegFileChooser.setAcceptAllFileFilterUsed(false);
jpegFileChooser.setMultiSelectionEnabled(true);
}
return jpegFileChooser;
}
/**
* @return Returns the btnCancel property.
*/
public JButton getBtnCancel() {
if (btnCancel == null) {
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new AbstractAction(){
public void actionPerformed(ActionEvent ae){System.exit(0);}
});
}
return btnCancel;
}
protected void showJpegFileChooser(){
getJpegFileChooser().setSelectedFile(null); //?
getJpegFileChooser().setSelectedFiles(null); //?
int status = getJpegFileChooser().showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
// stuff
}
}
}
Thanks for any advice.
Rick
Rick - 26 May 2004 12:36 GMT
I figured out a way to reset (clear) the selected files before
redisplaying my JFileChooser.
File file = new File("");
File[] files = {file};
chooser.setSelectedFile(file); //My mistake was
chooser.setSelectedFiles(files); //passing null to these methods.
chooser.showOpenDialog(this);
this worked for me. Please advise if there is a more 'correct'
solution.
Rick.
Roedy Green - 26 May 2004 20:09 GMT
>File file = new File("");
>File[] files = {file};
[quoted text clipped - 6 lines]
>this worked for me. Please advise if there is a more 'correct'
>solution.
you might try File[] noFiles = new File[0];

Signature
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.