Java Reference
In-Depth Information
Figure 9-28. A JFileChooser with an accessory panel
Listing 9-16 shows source for an Image component that displays an accessory icon. The
selected image file becomes the icon for a JLabel component. The component does two scaling
operations to make sure the dimensions of the image are sized to fit within the accessory.
Listing 9-16. Custom Accessory for Use with JFileChooser
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.io.*;
public class LabelAccessory extends JLabel implements PropertyChangeListener {
private static final int PREFERRED_WIDTH = 125;
private static final int PREFERRED_HEIGHT = 100;
public LabelAccessory(JFileChooser chooser) {
setVerticalAlignment(JLabel.CENTER);
setHorizontalAlignment(JLabel.CENTER);
chooser.addPropertyChangeListener(this);
setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
}
public void propertyChange(PropertyChangeEvent changeEvent) {
String changeName = changeEvent.getPropertyName();
if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
File file = (File)changeEvent.getNewValue();
if (file != null) {
ImageIcon icon = new ImageIcon(file.getPath());
if (icon.getIconWidth() > PREFERRED_WIDTH) {
icon = new ImageIcon(icon.getImage().getScaledInstance(
PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
Search WWH ::




Custom Search