I have a JDialog which calls an AsbtractAction which brings up a JFileChooser so the user can select a directory. These are all separate classes. What is the proper way of passing a value from the JFileChooser so I can display the path to the directory in the JDialog?
Edit: Updated the question.
This is an incomplete example but I guess can give you an idea of how to achieve what you need. The important bit is to reference the property where you want the selection back like YourDialog.this.selectedFile=file;
See below how to place it in the code:
public class YourDialog extends JDialog implements ActionListener {
protected File selectedFile=null;
//Constructor
public YourDialog(JFrame frame, boolean modal, String message) {
//... create button and added to the panel
someButton.addActionListener(new AbstractAction {
public void actionPerformed(ActionEvent e) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(YourDialog.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
// HERE IS THE TRICK I GUESS !!
YourDialog.this.selectedFile=file;
}
}
});
}
}
I hope it helps and sorry for not posting a full example.
Edit
In essence we are not passing any parameters to the AbstractAction. The fact is that the AbstractAction can access any non-private properties of the "caller" by accessing like YourDialog.this.somePropertyOrMethod. This is because the AbstractAction is an anonymous class of YourDialog class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With