Michael Thomas Flanagan's Java Scientific Library

FileNameSelector Class:       GUI for Selecting a File Name

     

Last update: 30 November 2005
Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains methods for opening a dialog window through which a file name can be selected for use within a program. The selected file is NOT OPENED. See FileChooser for methods that also open the file for reading.

import directive: import flanagan.io.FileNameSelector;
Related classes:

SUMMARY OF CONSTRUCTOR AND METHODS

Constructors public FileNameSelector()
public FileNameSelector(String path)
File extension filters public void setExtension(String extension)
public void setAllExtensions()
public String getExtension()
Directory path public void setPath(String path)
public String getPath()
Open an Open File dialog window and select a file name public String selectFile()
public String selectFile(String windowTitle)
Get the file name public String getFileName()
Get the file name without its extension public String getStemName()
Get the path name public String getPathName()
Get the directory path public String getDirPath()
File existence status public boolean fileFound()
Close the file public final synchronized close()
End the program public static final synchronized void.endProgram()
System.exit(0)

CONSTRUCTORS AND METHODS

CONSTRUCTORS
public FileNameSelector()
public FileNameSelector(String path)
Usage:                      FileNameSelector fc = new FileNameSelector();
Creates a new instance of FileNameSelector. When selectFile is called the directory displayed in the Open File dialog window will be the home directory, e.g. My Documents if a Microsoft operating system is being used. The window allows all non-hidden directories to be browsed. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).

Usage:                      FileNameSelector fc = new FileNameSelector(path);
Creates a new instance of FileNameSelector. When selectFile is called the directory displayed in the Open File dialog window will be that defined by the argument, path. The format of the path String must conform to the convention of the operating system, e.g. if a Microsoft Systems is being used, an example of path would be "C:\\Java_Programming\\flanagan_library\\io_files". The window allows all non-hidden directories to be browsed. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).



FILE NAME EXTENSION FILTERS
Set a file name extension filter
public void setExtension(String extension)
Usage:                      fc.setExtension(extension);
If this method is called before selectFile is called the Open File dialog window will display only folders and files with the same extenstion as that passed as the String argument, extension. The format of this String should be the extension, e.g, "txt", "doc", "xls", "ddt", ... If setExtension is not called, the Open File dialog window displays all folders and all files.

Remove the file name extension filter
public void setAllExtensions()
Usage:                      fc.setAllExtensions();
This method removes any file extension filter (see immediately above). After this method is called the Open File dialog window will display all folders and all files.

Get the current file name extension filter
public String getExtension()
Usage:                      extension = fc.getExtension();
This method returns the extension applied by a current extension filter, e.g. "txt". It returns null if no extension filter is currently operating.

DIRECTORY PATH
Reset the path to the initially displayed directory
public void setPath(String extension)
Usage:                      fc.setPath(path);
This method allows the path to the directory displayed, on opening, by selectFile to be reset. The format of the path String must conform to the convention of the operating system, e.g. if a Microsoft Systems is being used, an example of path would be "C:\\Java_Programming\\flanagan_library\\io_files".

See inherited method, getDirPath() (below), for method returning the path to the directory selected.

DISPLAY AN OPEN FILE DIALOG WINDOW AND CHOOSE A FILE NAME
public String selectFile()
public String selectFile(String windowTitle)
Usage:                      filename = fc.selectFile();
Displays an Open File dialog window. If no path argument was passed to the constructor the directory displayed in the Open File dialog window will be the home directory, e.g. My Documents if a Microsoft operating system is being used. The window allows all non-hidden directories to be browsed. On selecting a file its file name is returned as a string. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command
Remember you must also close the file opened once you have finished reading from it (see close() method).

Usage:                      filename = fc.selectFile(windowTitle);
Displays an Open File dialog window. If no path argument was passed to the constructor the directory displayed in the Open File dialog window will be the home directory, e.g. My Documents if a Microsoft operating system is being used. The window allows all non-hidden directories to be browsed. The argument windowTitle is displayed as the Open File dialog window title. This may be used as a prompt message line, e.g. "Open first data input file". The default title is "Select a file". On selecting a file its file name is returned as a string. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).

ENDING AN APPLICATION USING METHODS FROM CLASS FileNameSelector
public static final synchronized void EndProgram()
Usage:
                     fc.endProgram();

This method displays a dialogue box that asks if you want to end the program. Answering yes ends the program. Answering no displays a message telling you how you may now end the program.

Any application that you write that displays a graphical user interface, as the methods in this class Db do, must be terminated with the line
System.exit(0)
or by calling a method that contains that statement, e.g. by calling endProgram().



GETTING THE SELECTED PATH
public String getFilePath()
Usage:                      String path = fc.getPathName();
A method for returning the path name, e.g. "C:\JavaProg\Experiments\Exp1\inputData2.txt".

GETTING THE SELECTED FILE NAME
public String getFileName()
Usage:                      String name = fc.getFileName();
A method for returning the file name, e.g. "inputData2.txt". SelectFile also returns the file name.

GETTING THE SELECTED FILE NAME WITHOUT ITS EXTENSION
public String getStemName()
Usage:                      String name = fc.getStemName();
A method for returning the file name, e.g. "inputData2".

GETTING THE PATH TO THE DIRECTORY HOLDING THE INPUT FILE
public String getDirPath()
Usage:                      String dir = fin.getDirPath();
A method for returning the path to the directory holding bthe input file, e.g. "C:\JavaProg\Experiments\Exp1" where the full path name is "C:\JavaProg\Experiments\Exp1\inputData2.txt".

CLOSING THE FILE
public final synchronized close()
Usage:                      fc.close();
A method for closing the text file opened by fc.



FILE EXISTENCE STATUS
public boolean fileFound()
Usage:                      boolean ffd = fc.fileFound();
Returns true if the file whose filename has been passed to the instance of FileInput, fin, was found. Returns false if that file was not found.

OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:


This page was prepared by Dr Michael Thomas Flanagan