Java JFileChooser

JFileChooser is a class that is present in the java Swing package. The java Swing package is essential for JavaTM Foundation Classes(JFC). JFileChooser contains many elements that assist in building a graphical user Interface in java. Java Swing gives components like buttons, panels, dialogs, etc. JFileChooser is a simple and successful method for inciting the client to pick a file or a directory. JFileChooser inherited the properties of JComponent class and implemented them with an Accessible interface.

Constructors Present in Java JFileChooser Class

1. JFileChooser():

Constructs a JFileChooser highlighting the client's default directory.

Example program on JFileChooser()

Output:

Java JFileChooser

2. JFileChooser(File currentDirectory):

Constructs a JFileChooser involving the given File as the way.

Example program on JFileChooser(File currentDirectory)

Output:

Java JFileChooser

3. JFileChooser(File currentDirectory, FileSystemViewfsv):

Constructs a JFileChooser utilizing the given current catalog and FileSystemView.

Example program on JFileChooser(File currentDirectory, FileSystemViewfsv)

Output:

Java JFileChooser

4. JFileChooser(FileSystemViewfsv):

Constructs a JFileChooser utilizing the given FileSystemView.

Example program on JFileChooser(FileSystemViewfsv)

Output:

Java JFileChooser

5. JFileChooser(String currentDirectoryPath):

Constructs a JFileChooser utilizing the given way.

Example program on JFileChooser(String currentDirectoryPath):

Output:

Java JFileChooser

6. JFileChooser(String currentDirectoryPath, FileSystemViewfsv):

Constructs a JFileChooser utilizing the given current catalog way and FileSystemView.

Example program on JFileChooser( String currentDirectoryPath, FileSystemViewfsv ):

Output:

Java JFileChooser

7. JFileChooser( File ):

Example program on JFileChooser( File ) constructor:

Output:

Java JFileChooser

8. JFileChooser(File, FileSystemView):

Example on JFileChooser( File, FileSystemView )

Output:

Java JFileChooser

Advantages of JFileChooser()

  1. Statement of the JFileChooser() beyond the occasion audience additionally can be used within the occasion audience.
  2. JFileChooser return esteem, which portrays regardless of whether the record has been picked.
  3. Boundary given to the accompanying strategy for JFileChooser can confine clients effectively to choose either record or envelope or both.
  4. The JFileChooser() declaration made external to the event listener can moreover be utilized inside the event listener.
  5. The JFileChooser return result shows whether the file has been chosen.
  6. The accompanying JFileChooser technique's boundary permits clients to handily be limited from choosing either a file, a folder, or both.

Methods in JFileChooser Class

The JFileChooser class fundamentally gives three strategies that show the record chooser discoursed, as in the accompanying:

1. showOpenDialog(component owner ): This technique opens an exchange box that contains an endorsement button, as such, "Open", that opens a document from the discourse.

Syntax:

2. showSaveDialog( component owner ): This strategy is utilized when the client needs to save the particular document that he/she is utilizing. This exchange box contains an endorsement button, as such, "Save", to save the document.

Syntax:

3. showDialog(component owner, string): This technique shows an exchange box with an endorsement button which is client characterized.

Syntax:

4. getSelectedFile(): This technique returns the document that is chosen by the client.

Syntax:

5. setCurrentDirectory(File f): This technique sets the ongoing catalog as determined.

Syntax:

6. getCurrentDirectory(): This technique for JFileChooser returns the ongoing index as chosen by the client.

Syntax:

7. (File f):This strategy returns the name of the document as given by the record contention.

Syntax:

Example

A example program to create a JFrame window:

Output:

Java JFileChooser
Java JFileChooser
Java JFileChooser
Next TopicWhat is LINQ?