Example usage for javax.swing JFileChooser addPropertyChangeListener

List of usage examples for javax.swing JFileChooser addPropertyChangeListener

Introduction

In this page you can find the example usage for javax.swing JFileChooser addPropertyChangeListener.

Prototype

public void addPropertyChangeListener(PropertyChangeListener listener) 

Source Link

Document

Adds a PropertyChangeListener to the listener list.

Usage

From source file:Main.java

public static void main(String[] argv) {

    JFileChooser chooser = new JFileChooser();

    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();

                System.out.println(oldFile);
                System.out.println(newFile);
                System.out.println(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();

                Arrays.toString(oldFiles);
                Arrays.toString(newFiles);
                File[] files = chooser.getSelectedFiles();
                Arrays.toString(files);
            }/*  w w  w.  jav a2s  .  c o m*/
        }
    });

    chooser.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) {
    final JFileChooser chooser = new JFileChooser();

    File curDir = chooser.getCurrentDirectory();
    chooser.setDialogTitle("" + curDir.getAbsolutePath());

    chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
                File curDir = chooser.getCurrentDirectory();

                chooser.setDialogTitle("" + curDir.getAbsolutePath());
            }//  ww w  .j a  v a  2 s. c om
        }
    });
}

From source file:Main.java

public MyAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(50, 50));
}

From source file:MainClass.java

public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
}

From source file:FileChooserTest.java

/**
 * Constructs an ImagePreviewer./*from  w  ww.  j a  va2 s  .co  m*/
 * @param chooser the file chooser whose property changes trigger an image change in this
 * previewer
 */
public ImagePreviewer(JFileChooser chooser) {
    setPreferredSize(new Dimension(100, 100));
    setBorder(BorderFactory.createEtchedBorder());

    chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
                // the user has selected a new file
                File f = (File) event.getNewValue();
                if (f == null) {
                    setIcon(null);
                    return;
                }

                // read the image into an icon
                ImageIcon icon = new ImageIcon(f.getPath());

                // if the icon is too large to fit, scale it
                if (icon.getIconWidth() > getWidth())
                    icon = new ImageIcon(
                            icon.getImage().getScaledInstance(getWidth(), -1, Image.SCALE_DEFAULT));

                setIcon(icon);
            }
        }
    });
}

From source file:FilterSample.java

public LabelAccessory(JFileChooser chooser) {
    setVerticalAlignment(JLabel.CENTER);
    setHorizontalAlignment(JLabel.CENTER);
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
}

From source file:FileChooserDemo2.java

public ImagePreview(JFileChooser fc) {
    setPreferredSize(new Dimension(100, 50));
    fc.addPropertyChangeListener(this);
}

From source file:ch.fork.AdHocRailway.ui.locomotives.configuration.LocomotiveConfig.java

public void chooseLocoImage() {
    File previousLocoDir = ctx.getPreviousLocoDir();
    if (previousLocoDir == null) {
        previousLocoDir = new File("locoimages");
    }/*www  .ja  v a 2s  .  c  o m*/
    final JFileChooser chooser = new JFileChooser(previousLocoDir);

    final ImagePreviewPanel preview = new ImagePreviewPanel();
    chooser.setAccessory(preview);
    chooser.addPropertyChangeListener(preview);

    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Image Files";
        }

        @Override
        public boolean accept(final File f) {
            if (f.isDirectory()) {
                return true;
            }
            if (StringUtils.endsWithAny(f.getName().toLowerCase(), ".png", ".gif", ".bmp", ".jpg")) {
                return true;
            }
            return false;
        }
    });

    final int ret = chooser.showOpenDialog(LocomotiveConfig.this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File selectedFile = chooser.getSelectedFile();
        ctx.setPreviousLocoDir(selectedFile.getParentFile());
        presentationModel.getBean().setImage(selectedFile.getName());
        final String image = presentationModel.getBean().getImage();
        presentationModel.getBean()
                .setImageBase64(LocomotiveImageHelper.getImageBase64(presentationModel.getBean()));
        if (image != null && !image.isEmpty()) {
            imageLabel.setIcon(LocomotiveImageHelper.getLocomotiveIcon(presentationModel.getBean()));
            pack();
        } else {
            imageLabel.setIcon(null);
            pack();
        }
    }
}