edgarmtze
edgarmtze

Reputation: 25038

Get the path of chosen file java

How to display a JFileChooser so I get the absolute path of a file, then assign the path to a string

I was doing something like:

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

Upvotes: 0

Views: 7242

Answers (3)

Jeel Shah
Jeel Shah

Reputation: 3324

File f = chooser.getSelectedFile();

String path = f.getAbsolutePath();

Upvotes: 0

David Oliván
David Oliván

Reputation: 2725

You need to complete like:

int option = chooser.showOpenDialog(parentComponent); // parentComponent must a component like JFrame, JDialog...
if (option == JFileChooser.APPROVE_OPTION) {
   File selectedFile = chooser.getSelectedFile();
   String path = selectedFile.getAbsolutePath();
}

Upvotes: 2

Jim Garrison
Jim Garrison

Reputation: 86754

File f = chooser.getSelectedFile();
String absPath = f.getAbsolutePath();

Upvotes: 2

Related Questions