Swing: process JFileChooser result after action?

痴心易碎 提交于 2021-01-29 05:11:15

问题


I want to use an Abstract action for JFileChooser buttons because there will be dozens of these.

public class OpenFileAction extends AbstractAction {
JFrame frame;
JFileChooser chooser;

OpenFileAction(JFrame frame, JFileChooser chooser) {
    super("Open...");
    this.chooser = chooser;
    this.frame = frame;
}

public void actionPerformed(ActionEvent evt) {
    // Show dialog; this method does not return until dialog is closed
    chooser.showOpenDialog(frame);
}
};

Obviously I want to write the JFileChooser result to a variable. How can I access e.getSource() AFTER the action is finished? This does not work because it is triggered before the FileChooser Dialog opens:

    JButton btnNewButton_1 = new JButton(new OpenFileAction(new JFrame(), new JFileChooser(new File(".")) ) );
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //process e.getSource() ?
        }
    });

回答1:


I think what you're after is the following:

public abstract class OpenFileAction extends AbstractAction {
    JFrame frame;
    JFileChooser chooser;

    public OpenFileAction(JFrame frame, JFileChooser chooser) {
        super("Open...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        int option = chooser.showOpenDialog(this.frame);
        if (option == JFileChooser.APPROVE_OPTION) {
            File selectedFile = chooser.getSelectedFile();
            doWithSelectedFile(selectedFile)
        }
    }

    /**
     * Method to override, which gets called with the selected file.
     */
    protected abstract doWithSelectedFile(File file);
}

...

OpenFileAction action = new OpenFileAction(frame, new JFileChooser(new File("."))) {
    @Override
    protected void doWithSelectedFile(File file) {
        // do what you want here
    }
};
JButton button = new JButton(action);



回答2:


Here is an update of your code showing how you can work this out. Your action listener is not needed since you already set an Action on the JButton (which is also an ActionListener).

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestFileChooser {

    public static class OpenFileAction extends AbstractAction {
        JFileChooser chooser;

        OpenFileAction(JFileChooser chooser) {
            super("Open...");
            this.chooser = chooser;
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            int retval = chooser.showOpenDialog((Component) evt.getSource());
            if (retval == JFileChooser.APPROVE_OPTION) {
                File selectedFile = chooser.getSelectedFile();
                JOptionPane.showMessageDialog((Component) evt.getSource(), "You have chosen " + selectedFile.getAbsolutePath());
            }
        }
    };

    protected void initUI() {
        JFrame frame = new JFrame("Test file chooser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser chooser = new JFileChooser(new File("."));
        JButton btnNewButton_1 = new JButton(new OpenFileAction(chooser));
        frame.add(btnNewButton_1);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestFileChooser().initUI();
            }
        });
    }
}

Here is the tutorial on JFileChooser which may provide additional information.




回答3:


EDIT:

the following code is apt for you:

JButton openButton = new JButton();
openButton.addActionListener(new ActionListener(){
    public void actionPerformed(evt){
        final JFileChooser fileChooser = new JFileChooser();
        File openFile;
        if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            openFile= fileChooser.getSelectedFile();
        //place your code here
    }
});

OR (if you absolutely need a local method)

JButton openButton = new JButton();
openButton.addActionListener(new ActionListener(){
    public void actionPerformed(evt){
        final JFileChooser fileChooser = new JFileChooser();
        if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            openButtonActionPerformed(fileChooser.getSelectedFile());
    }
});
openButtonActionPerformed(File openFile){
    //place your code here
}

There are no need to create another class



来源:https://stackoverflow.com/questions/11257593/swing-process-jfilechooser-result-after-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!