How to get directory path using JFileChooser?
Asked Answered
U

2

9

I have a small java GUI application with a text field on it. When the user clicks the text field an event is triggered and the JFileChooser is launched. It's restricted to directories only.

What I'm trying to do is to get the full path of the directory that was chosen and put it in the text field.

I have no idea how to do this, I've searched through a ton of java tutorials and documentations and I can't find an answer. Can someone help me?

To clarify: I want to get the full path as a string and put it in the text field, overwriting anything that was there before.

Unsubstantial answered 25/10, 2010 at 22:46 Comment(0)
E
21

Try something like

myTextField.setText(myFileChooser.getSelectedFile().getAbsolutePath());

What you are doing there is retrieving the File object from the file chooser, then grabbing its path and throwing it into the text field.

Electrotherapy answered 25/10, 2010 at 22:49 Comment(1)
Thank you! I've only recently started learning java so I wasn't sure what to do... now it seems so obvious :PUnsubstantial
T
6

Check out the JFileChooser.getCurrentDirectory() function:

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory())

Travail answered 25/10, 2010 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.