- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the path of the file selected in the JFileChooser component with Java
To get the path of the selected file, at first get the selected file −
java.io.File f = file.getSelectedFile();
Now, get the path of the selected file which we will get using the above method −
System.err.println(f.getPath());
The following is an example to get the path of the file selected in the JFileChooser component −
Example
package my; import javax.swing.JFileChooser; public class SwingDemo { public static void main(String[] args) { JFileChooser file = new JFileChooser(); file.setMultiSelectionEnabled(true); file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); file.setFileHidingEnabled(false); if (file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { java.io.File f = file.getSelectedFile(); System.err.println(f.getPath()); } } }
Output
On clicking “Open” above, the path of the selected file is displayed in the Console −
- Related Articles
- Get the name of the file and path in Java
- Java Program to get the File object with the absolute path for the directory or file
- Get the absolute path for the directory or file in Java
- Get Absolute path of a file in Java
- Get the Full Path of a File in Linux
- How to get left substring in MySQL from a column with file path? Display the entire file path string excluding the file name?
- How to get the file name from the file path in Python?
- How to get Directories from JFileChooser in Java
- File Path with double slash in Java
- Get the Component Type of an Array Object in Java
- Golang program to get the name of the file from the absolute path
- C# Program to get the name of the file from the absolute path
- Java Program to remove file information from a filename returning only its path component
- Java Program to remove path information from a filename returning only its file component
- How to get the leaf after this node in a JTree component with Java?

Advertisements