开发者

Can actionPerformed return a value?

开发者 https://www.devze.com 2022-12-27 21:45 出处:网络
In my application I use a FileCh开发者_如何学Cooser to chose a file. The name of the selected file should be returned to another class. how to do this in eclipse?actionPerformed is called by the event

In my application I use a FileCh开发者_如何学Cooser to chose a file. The name of the selected file should be returned to another class. how to do this in eclipse?


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;
    }
}


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


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
                }
            }});
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号