views:

144

answers:

3

In my application I use a FileChooser to chose a file. The name of the selected file should be returned to another class. how to do this in eclipse?

A: 

Have a look at FileChooserDemo and FileChooserDemo2 here for how FileChooser is used.

Here is a relevant excerpt of the code:

    public void actionPerformed(ActionEvent e) {
    //Set up the file chooser.
    if (fc == null) {
        fc = new JFileChooser();

    //Add a custom file filter and disable the default
    //(Accept All) file filter.
        fc.addChoosableFileFilter(new ImageFilter());
        fc.setAcceptAllFileFilterUsed(false);

    //Add custom icons for file types.
        fc.setFileView(new ImageFileView());

    //Add the preview pane.
        fc.setAccessory(new ImagePreview(fc));
    }

    //Show it.
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");

    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        log.append("Attaching file: " + file.getName()
                   + "." + newline);
    } else {
        log.append("Attachment cancelled by user." + newline);
    }
    log.setCaretPosition(log.getDocument().getLength());

    //Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
}
Oren
A: 

Assuming class "A" contains the code to show the file chooser and class "B" needs the value, the following will do what you need.

class A {
    private PropertyChangerSupport changer = new PropertyChangerSupport(this);
    private File selectedFile = null;

    public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.addPropertyChangeListener(property, listener);
    }

    public void removePropertyChangeListener(String property, PropertyChangeListener listener) {
        changer.removePropertyChangeListener(property, listener);
    }

    public void actionPerformed(ActionEvent evt) {
        // Prompt the user for the file
        selectedFile = fc.getSelectedFile();
        changer.firePropertyChange(SELECTED_FILE_PROP, null, selectedFile);
    }
}

class B {
    public B(...) {
        // ...
        A a = ...
        a.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChanged(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(A.SELECTED_FILE_PROP)) {
                    File selectedFile = (File)evt.getNewValue();
                    // Do something with selectedFile
                }
            }});
    }
}
Devon_C_Miller
+1  A: 

actionPerformed is called by the event dispatch thread when some event (a button was clicked for instance) and it should never be invoked directly. If you want a method that shows a FileChooser and returns the selected file, then declare another method that can be called by an eventHandler as well as anywhere else:

public void actionPerformed(ActionEvent e) {
    File myFile = selectFile();
    doSomethingWith(myFile);
}

public File selectFile() {
    int returnVal = fc.showDialog(FileChooserDemo2.this,
                                  "Attach");
    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    } else {
        return null;
    }
}
Maurice Perry