| |
e899. Setting an Accessory Component in a JFileChooser Dialog
The accessory is an optional component that can be added to a file
chooser dialog to show a preview image of the selected file. By default,
a file chooser dialog does not have an accessory installed. This
example demonstrates how to install an accessory component.
JFileChooser chooser = new JFileChooser();
// Create and set the accessory
chooser.setAccessory(new MyAccessory(chooser));
// Show the dialog; wait until dialog is closed
chooser.showOpenDialog(frame);
public class MyAccessory extends JComponent implements PropertyChangeListener {
public MyAccessory(JFileChooser chooser) {
// Listen for changes to the selected file
chooser.addPropertyChangeListener(this);
// Set a preferred size
setPreferredSize(new Dimension(50, 50));
}
// This listener listens for changes to the selected file
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(
evt.getPropertyName())) {
JFileChooser chooser = (JFileChooser)evt.getSource();
// Get the new selected file
File newFile = (File)evt.getNewValue();
// Prepare the preview data based on the new selected file
// Repaint this component
repaint();
}
}
public void paint(Graphics g) {
// Paint a preview of the selected file
}
}
e898.
Changing the Text of the Approve Button in a JFileChooser Dialog
e900.
Displaying the Current Directory in the Title of a JFileChooser Dialog
© 2002 Addison-Wesley.
| | |