Getting and Setting the Selected File of a JFileChooser Dialog - Java Swing

Java examples for Swing:JFileChooser

Description

Getting and Setting the Selected File of a JFileChooser Dialog

Demo Code

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();

    try {//w  w w .  j a v  a 2s.c  o  m
      File f = new File(new File("filename.txt").getCanonicalPath());

      // Set the selected file
      chooser.setSelectedFile(f);
    } catch (Exception e) {
    }

    chooser.showOpenDialog(new JFrame());

    // Get the currently selected file
    File curFile = chooser.getSelectedFile();
  }
}

Related Tutorials