Java JFileChooser .showDialog (Component parent, String approveButtonText)

Syntax

JFileChooser.showDialog(Component parent, String approveButtonText) has the following syntax.

public int showDialog(Component parent,  String approveButtonText)  throws HeadlessException

Example

In the following code shows how to use JFileChooser.showDialog(Component parent, String approveButtonText) method.


//w w w  .  j  av  a 2  s.com
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] argv) throws Exception{
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());
    
    chooser.setSelectedFiles(new File[]{f});
    chooser.showDialog(new JFrame(""), null);
    File curFile = chooser.getSelectedFile();
  }
}