actionPerformed可以返回一个值吗?

时间:2010-04-19 11:07:07

标签: eclipse swing return-value jfilechooser

在我的应用程序中,我使用JFileChooser来选择文件。应将所选文件的名称返回给另一个类。如何在日食中做到这一点?

3 个答案:

答案 0 :(得分:3)

事件调度线程在某些事件(例如单击一个按钮)时调用了actionPerformed,并且永远不应该直接调用它。如果你想要一个显示FileChooser并返回所选文件的方法,那么声明一个可以被eventHandler以及其他任何地方调用的方法:

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

答案 1 :(得分:0)

查看FileChooserDemo和FileChooserDemo2 here了解FileChooser的使用方法。

以下是代码的相关摘录:

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

答案 2 :(得分:0)

假设类“A”包含显示文件选择器的代码而类“B”需要该值,则以下内容将满足您的需要。

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