Reputation: 5064
Currently I am using Netbeans. I have added a jFileChooser in a jFrame. All is ok, but when I select a file and click on the Open button of the jFileChooser it happens nothings. I want to get the selected file's address path when the button is clicked. How can I write code for the button?
Upvotes: 0
Views: 9205
Reputation: 21
If you added the JFileChooser
control to your JFrame
, you should not instantiate another JFileChooser
. Just add two lines:
JFileChooser chooser = (JFileChooser) evt.getSource();
and the line that Asier Aranbarri gave in his answer and use your variable name (e.g. chooser
) instead of myFileChooser
.
By the way, if you want to know whether the Open or the Cancel button was pressed get the event command:
String command = evt.getCommand();
The string will either contain "ApproveSelection"
(Open button) or "CancelSelection"
(Close button).
Upvotes: 0
Reputation: 205785
Check the chooser's return value. If it's the APPROVE_OPTION
, getSelectedFile()
will return the selected File
. This complete example follows API almost verbatim in ImageOpenAction
.
Upvotes: 1
Reputation: 11830
You could try something like this when the listener of the button is activated:
String filePath = myFileChooser.getSelectedFile().getAbsolutePath();
Of course, you may not want to store it in a String, but hey, just an example.
Upvotes: 1