Skip to main content
Engineering LibreTexts

11.6: From the Java Library- javax.swing.JFileChooser

  • Page ID
    15131
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    class is useful for dealing with files and directories in a GUI environment. You are probably already familiar with JFileChoosers, although you may not have known them by that name. A JFileChooser provides a dialog box that enables the user to select a file and a directory when opening or saving a file. Figure [fig-opendialog] shows an example.

    A JFileChooser is designed primarily to be used in conjunction with menu-based programs. The JFileChooser class (Fig. 11.29) contains methods that support the Open File and Save As options which often appear in GUI applications either in a menu or attached to buttons. In this section we provide the basics for using a JFileChooser. Options for opening a file or saving a file can be added to the kind of GUI applications that we encountered earlier in the text by using buttons. In Chapter 13, we will discuss the use of JMenus which will provide a more natural means of using the JFileChooser dialogs.

    A JFileChooser is not itself the dialog window, but rather the object that manages the dialog. After creating a JFileChooser instance, its showOpenDialog() or showSaveDialog() methods are used to open a dialog window. Note that these methods require a Component parameter, usually a JFrame or a JApplet. Thus, JFileChoosers can be used only in GUI applications and applets.

    To illustrate how to use a JFileChooser, let’s consider the case where the user has selected an Open File menu item or clicked a button labeled Open File. In this case, executing the following code will cause an “Open File” dialog to appear:

    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(this);
    
    if (result == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        // Insert code here to read data from file
        String fileName = file.getName();
        display.setText("You selected " + fileName);
    } else
        display.setText("You cancelled the file dialog");

    We begin by creating a JFileChooser and then telling it to showOpenDialog(). If we were saving a file rather than opening one, we would tell it to showSaveDialog(). In either case, a dialog window will pop up on the screen. The dialog assists the user in navigating through the file system and selecting a file (Fig. [fig-opendialog]).

    The dialog contains two buttons, one labeled Open and the other labeled Cancel. If the user selects a file, that choice will correspond to APPROVE_OPTION. If the user cancels the dialog, that will correspond to CANCEL_OPTION. After opening a dialog, the code should check which option resulted. In this case, if the user opened a file, the code gets a reference to the file and then simply uses that to print the file’s path name to a text area named display. In an actual application, code would be inserted at that spot which uses the file reference to read data from the file.


    This page titled 11.6: From the Java Library- javax.swing.JFileChooser is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Ralph Morelli & Ralph Wade via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.