Setting an Accessory Component in a JFileChooser Dialog
import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
public class Main {
public static void main(String[] argv) {
JFileChooser chooser = new JFileChooser();
chooser.setAccessory(new MyAccessory(chooser));
chooser.showOpenDialog(null);
}
}
class MyAccessory extends JComponent implements PropertyChangeListener {
public MyAccessory(JFileChooser chooser) {
chooser.addPropertyChangeListener(this);
setPreferredSize(new Dimension(50, 50));
}
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();
repaint();
}
}
public void paint(Graphics g) {
// Paint a preview of the selected file
}
}
Related examples in the same category