Here is the simpliest way to choose a file path in Java :
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(YourClass.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
You can plug this actionPerformed to a button for example, and that's it. Your button will open a GUI to select a file, and if the user select the file JFileChooser.APPROVE_OPTION
, then you can perform the action that you want (here only logging what was opened)
See the oracle documentation (https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html) if you want to do something else (not binding a button ?) or something more complex (filter for some extensions only ? ...)