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() ?
}
});
I think what you’re after is the following: