![]() |
The Java Developers Almanac 1.4 |
|
e903. Listening for Changes to the Selected File in a JFileChooser DialogA property change event is fired whenever the selected file is changed. However, the selected file can be null if the selected item does not match the selection mode of the chooser. For example, if the selection mode isJFileChooser.FILES_ONLY and a directory is
selected, the fired event will have a new value of null.
Note: When in multiple-selection mode,
JFileChooser chooser = new JFileChooser();
// Add listener on chooser to detect changes to selected file
chooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
.equals(evt.getPropertyName())) {
JFileChooser chooser = (JFileChooser)evt.getSource();
File oldFile = (File)evt.getOldValue();
File newFile = (File)evt.getNewValue();
// The selected file should always be the same as newFile
File curFile = chooser.getSelectedFile();
} else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(
evt.getPropertyName())) {
JFileChooser chooser = (JFileChooser)evt.getSource();
File[] oldFiles = (File[])evt.getOldValue();
File[] newFiles = (File[])evt.getNewValue();
// Get list of selected files
// The selected files should always be the same as newFiles
File[] files = chooser.getSelectedFiles();
}
}
}) ;
e905. Listening for Approve and Cancel Events in a JFileChooser Dialog
© 2002 Addison-Wesley. |