WebNFS Developer's Guide

Setting the Accessory

The XFileChooserDemo's accessory component is a small panel on the right side of the file chooser that displays a preview of GIF and JPEG graphics files. In Figure 5-9 it's the picture of William Shakespeare:

Figure 5-9 The XFileChooserDemo's Accessory

Graphic

Another use for an accessory is a panel with more controls in it (say, checkboxes to toggle certain features).

Two aspects of accessories are important:

The XFileChooserDemo program begins by creating the file chooser; the accessory (here called the previewer), which is declared to be a FilePreviewer object; and a related checkbox. It adds a listener (ActionListener) to the checkbox, as we've seen with the file view and file filtering:


chooser = new XFileChooser;
...
previewer = new FilePreviewer(chooser);
...
accessoryButton = new JCheckBox("Show Preview");
accessoryButton.addActionListener(optionListener);

The OptionListener() checks to see if the Show Preview checkbox is checked; if it is, it calls setAccessory():


if (c == accessoryButton) {
     if (AccessoryButton.isSelected()) {
          chooser.setAccessory(previewer);
     else
          chooser.setAccessory(null);

The previewer itself just sets its preferred size and adds a PropertyChangeListener() that listens for changes to the file chooser's state.

This is its only method, its constructor:


public FilePreviewr(XFileChooser fc) {
     setPreferredSize(new Dimension(100, 50));
     fc.addPropertyChangeListener(this);

If a property is changed, addPropertyChangeListener() calls the abstract method PropertyChange(). Here's how PropertyChange() defined for the XFileChooserDemo program. It first checks to see what property has changed; if a new file has been selected, then a new preview is required.


public void propertyChange(PropertyChangeEvent e) {
     String prop = e.getPropertyName();
     if (prop.equals(XFileChooser.SELECTED_XFILE_CHANGED_PROPERTY)) {
          f = (File) e.getNewValue()
          if (isShowing()) {
               loadImage();
               repaint();
          }
     }
}