File Dialogs  «Prev  Next»
Lesson 1

Introduction to Java File Dialogs

Now that you know how to use the File class and how to get information about files and directories, it is time to learn about creating file dialog boxes to let users choose files.
In this module, you will learn:
  1. How to create a file dialog box to select a file
  2. How to specify which types of files the file dialog box will accept

We will begin working on the course project in this module as well.

FileDialog

Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed.
This is the standard file dialog box provided by the operating system. Here are three FileDialog constructors:
FileDialog(Frame parent)
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
Here, parent is the owner of the dialog box. The boxName parameter specifies the name displayed in the box's title bar. If boxName is omitted, the title of the dialog box is empty.
If how is FileDialog.LOAD, then the box is selecting a file for reading. If how is FileDialog.SAVE, the box is selecting a file for writing. If how is omitted, the box is selecting a file for reading.

Example: JFC Class that allows you to select file.

// Demonstration of File dialog boxes.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileChooserTest extends JFrame {
 private JTextField filename = new JTextField(), 
 dir = new JTextField();
 private JButton open = new JButton("Open"), 
 save = new JButton("Save");

 public FileChooserTest() {
  JPanel p = new JPanel();
  open.addActionListener(new OpenL());
  p.add(open);
  save.addActionListener(new SaveL());
  p.add(save);
  Container cp = getContentPane();
  cp.add(p, BorderLayout.SOUTH);
  dir.setEditable(false);
  filename.setEditable(false);
  p = new JPanel();
  p.setLayout(new GridLayout(2, 1));
  p.add(filename);
  p.add(dir);
  cp.add(p, BorderLayout.NORTH);
 }

 class OpenL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   JFileChooser c = new JFileChooser();
   // Demonstrate "Open" dialog:
   int rVal = c.showOpenDialog(FileChooserTest.this);
   if (rVal == JFileChooser.APPROVE_OPTION) {
    filename.setText(c.getSelectedFile().getName());
    dir.setText(c.getCurrentDirectory().toString());
   }
   if (rVal == JFileChooser.CANCEL_OPTION) {
    filename.setText("You pressed cancel");
    dir.setText("");
   }
  }// end - actionPerformed
 } ///ActionListener

 class SaveL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   JFileChooser c = new JFileChooser();
   // Demonstrate "Save" dialog:
   int rVal = c.showSaveDialog(FileChooserTest.this);
   if (rVal == JFileChooser.APPROVE_OPTION) {
     filename.setText(c.getSelectedFile().getName());
     dir.setText(c.getCurrentDirectory().toString());
   }
   if (rVal == JFileChooser.CANCEL_OPTION) {
     filename.setText("You pressed cancel");
     dir.setText("");
   }
  }
 }
 public static void main(String[] args) {
   run(new FileChooserTest(), 250, 110);
 }
 public static void run(JFrame frame, int width, int height) {
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(width, height);
  frame.setVisible(true);
 }
}