-
Bug
-
Resolution: Fixed
-
P4
-
1.2.0
-
beta
-
generic
-
generic
Name: vi73552 Date: 05/04/99
If the approveSelection() method is overloaded on a custom
JFileChooser implementation and a custom FileView is also used
then it's possible for text that is manually entered by the user
to be modified without the user's knowledge. This will happen
if the FileView's getName() method is defined to alter the names
of files displayed in the chooser's JList and the approveSelection()
method does some error checking on the user's selection.
The following test program shows the problem. Basically it is set up
such that any files ending with ".junk" will have that part of
the name stripped off before it's displayed in the chooser's JList.
The approveSelection() method is defined to reject a file that ends
with ".junk", which prevents the chooser window from closing. The
problem is that the content of the JTextField is modified right
before the user's eyes for the case where approveSelection() rejects
the file. To see what I'm talking about run the test program and
then type in "myfile.junk" and click "Open". You should see that
the JTextField's content will be changed to "myfile" even though
the text was manually entered. This changing of text that had
been typed in by the user is unexpected and confusing. The only
place where file name changes should occur are within the JList
by way of using a custom FileView.
//~~~~~~~~~~~~~~~~~~~ content of chooserBug.java ~~~~~~~~~~~~~~~
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
//##############################################################
public class chooserBug extends JFrame {
public chooserBug() {
JButton btn = new JButton("Click Here");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myChooser fc = new myChooser();
fc.setFileView(new myFileView());
if (fc.showOpenDialog(null) == fc.APPROVE_OPTION)
System.out.println("YOU CHOSE: "+fc.getSelectedFile());
}
});
Container contentPane = getContentPane();
getContentPane().add(btn);
}
public static void main(String s[]) {
JFrame frame = new chooserBug();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.pack();
frame.setVisible(true);
}
}
//##############################################################
class myChooser extends JFileChooser {
public void approveSelection() {
if (getSelectedFile().toString().endsWith(".junk")) {
String msg = "Hey! Where did the \".junk\" extension\n" +
"that I just typed go to ???";
JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
return;
} else
super.approveSelection();
}
}
//##############################################################
class myFileView extends FileView {
public String getName(File f) {
if (f.toString().endsWith(".junk"))
// Strip off extension
return f.getName().substring(0, f.getName().lastIndexOf(".junk"));
else
return null; // let the L&F FileView figure this out
}
// don't care about the rest
public String getDescription(File f) { return null; }
public Boolean isTraversable(File f) { return null; }
public String getTypeDescription(File f) { return null; }
public Icon getIcon(File f) { return null; }
}
(Review ID: 57572)
======================================================================