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:
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class JFileChooserExample extends JFrame { //Sample 01: Create a Text Field to Show File Name JTextField txtFileName = new JTextField(45); public JFileChooserExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 750, 120); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); |
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.
1 2 3 4 5 6 7 |
//Sample 03: Add Controls to Frame Window JButton btnPickFile = new JButton("..."); JLabel lbl = new JLabel("File Name: "); txtFileName.setFont(new Font("Verdana", Font.ITALIC, 16)); ControlHost.add(lbl); ControlHost.add(txtFileName); ControlHost.add(btnPickFile); |
5. JFileChooser – APPROVE_OPTION, CANCEL_OPTION
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 04: Add Handler for Button btnPickFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser jfc = new JFileChooser("D:\\Java"); int UserChoice = jfc.showOpenDialog(JFileChooserExample.this); if (UserChoice == JFileChooser.APPROVE_OPTION) { File SelectedFile = jfc.getSelectedFile(); txtFileName.setText(SelectedFile.getPath()); } if (UserChoice == JFileChooser.CANCEL_OPTION) txtFileName.setText("No File Selected"); } }); |
6.Youtube Demo
7. Code Reference
7.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 08: Create Instance of JFrameDemo JFileChooserExample frame = new JFileChooserExample("JFileChooser Example"); frame.setVisible(true); } } |
7.2 JFileChooserExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class JFileChooserExample extends JFrame { //Sample 01: Create a Text Field to Show File Name JTextField txtFileName = new JTextField(45); public JFileChooserExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 750, 120); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 03: Add Controls to Frame Window JButton btnPickFile = new JButton("..."); JLabel lbl = new JLabel("File Name: "); txtFileName.setFont(new Font("Verdana", Font.ITALIC, 16)); ControlHost.add(lbl); ControlHost.add(txtFileName); ControlHost.add(btnPickFile); //Sample 04: Add Handler for Button btnPickFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser jfc = new JFileChooser("D:\\Java"); int UserChoice = jfc.showOpenDialog(JFileChooserExample.this); if (UserChoice == JFileChooser.APPROVE_OPTION) { File SelectedFile = jfc.getSelectedFile(); txtFileName.setText(SelectedFile.getPath()); } if (UserChoice == JFileChooser.CANCEL_OPTION) txtFileName.setText("No File Selected"); } }); } } |
Categories: Swing
Tags: APPROVE_OPTION, CANCEL_OPTION, getSelectedFile, JFileChooser, showOpenDialog