Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JFileChooser – Get Full File Path

1 About JFileChooser

JFileChooser is a Java Swing component which one can use to pick a file from the operating system. This component allows browsing the folders and pick a file from a specific folder. File open dialog blocks the calling code until the user closes the dialog either by clicking OK or Cancel.

2. About JFileChooser Example

The example which we will create is below:

JFileChooser Selected File Example
JFileChooser Selected File Example

When we launch the example, it will display a frame window with text field. The click of the ellipsis button (…) will open the file open dialog. From the displayed dialog, we can browse through the folders of the PC and pick a specific file on the folder structure. Our example will display the selected file in the text field. This happens only when the user clicks the Open button. When the user clicks the Cancel button, it will not show the picked file in the text field.

3. Prepare JFrame With FlowLayout

The FlowLayout Manager takes care of laying out the control for the frame window. In our case, the window will have only three controls. Since we need to access the text field when the user picks a file, we declared it as a class data member. Moreover, the text field is big enough to hold 45 characters. Note, the path and filename can extend up to 255 char-width and in that case the user should use the left and right arrow keys to see the full file path in the JTextField.

4. Add Controls

Next, we create JButton, JLabel and JTextField with a verdana font. After creating all the components, we add it to the JFrame and flow layout will take care of how to arrange them.


The showOpenDialog method of the JFileChooser will display the Java Swing’s file open dialog. Swing shows this dialog as a modal dialog, so the method is a blocking call. The calling thread will resume the execution when the user closes the dialog via the OK and Cancel button clicks. But how does the calling code know which button the user clicked to close the dialog?

One can compare the constants exposed by the JFileChooser with the code returned by the method showOpenDialog. The constant APPROVE_OPTION denotes that the user dismissed the dialog using the OK button. The CANCEL_OPTION tells that the user cancelled the file open dialog.

6. Get Selected File From JFileChooser

Ellipsis button in the JFrame handles the ActionEvent through an inner handler. At line 6, the handler constructs JFileChooser instance, and the constructor gets the initial path as string. When the dialog displays, it shows the initial folder D:\Temp and all files in that location.

At Line 7, the handler calls the method showOpenDialog, and it passes the JFrame as parent. This means, when the user closes the JFrame, java swing closes the Open File Dialog as well. This method is a blocking call and it will return when user dismisses the dialog. At line 8, we check the return value with JFileChooser constant APPROVE_OPTION.

At line 10, the getSelectedFile method call gives the file selected in string format. This string includes path and the file name. We assign this string containing the full file path to the JTextField.

6.Youtube Demo

JFileChooser Demo

7. Code Reference



Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.