charvax.swing

Class JFileChooser


public class JFileChooser
extends JComponent

The JFileChooser class displays a dialog from which the user can choose a file. The dialog is always modal (i.e. the user cannot interact with any other windows until he closes the dialog).

The dialog is displayed by calling its showDialog() method, which blocks until the dialog is closed (by the user pressing the Approve or Cancel buttons, or by pressing ENTER while the focus is in the "Filename" field). After the dialog has been closed, the program can find out what File was selected by calling the getSelectedFile() method.

The labels of the buttons that are displayed in the JFileChooser can be customized by changing the following static variables:

"Accelerator keys" can also be set for the buttons. For example, to set the F1 key to have the same effect as pressing the CANCEL button, call the following code before using the JFileChooser:

 	JFileChooser.CANCEL_LABEL = "Cancel (F1)";
 	JFileChooser.CANCEL_ACCELERATOR = KeyEvent.VK_F1;
 
Note that after the buttons have been customized, they stay customized for all future invocations of JFileChooser (until they are re-customized to some other value).

Field Summary

static int
APPROVE_ACCELERATOR
static String
APPROVE_LABEL
static int
APPROVE_OPTION
static int
CANCEL_ACCELERATOR
static String
CANCEL_LABEL
static int
CANCEL_OPTION
static int
DIRECTORIES_ONLY
static int
ERROR_OPTION
static int
FILES_AND_DIRECTORIES
static int
FILES_ONLY
static int
NEW_DIRECTORY_ACCELERATOR
static String
NEW_DIRECTORY_LABEL
static int
PARENT_DIRECTORY_ACCELERATOR
static String
PARENT_DIRECTORY_LABEL
protected static int
_COLS
protected static int
_ROWS
protected String
_approveButtonText
protected boolean
_cancelWasPressed
protected File
_currentDirectory
The current directory shown in the dialog.
protected charvax.swing.JFileChooser.DirList
_dirList
protected FileFilter
_fileFilter
protected int
_fileSelectionMode
protected Vector
_filelisteners
protected String
_location
protected String
_title

Fields inherited from class charvax.swing.JComponent

_border

Fields inherited from class charva.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT, _alignmentX, _alignmentY, _background, _cursesColor, _enabled, _focusListeners, _foreground, _keyListeners, _origin, _parent, _visible

Constructor Summary

JFileChooser()
Constructs a JFileChooser pointing to the user's home directory.
JFileChooser(File currentDirectory_)
Constructs a JFileChooser pointing to the specified directory.
JFileChooser(String currentDirectoryPath_)
Constructs a JFileChooser with the specified pathname.

Method Summary

protected void
addFileChooserListener(charvax.swing.JFileChooser.FileChooserListener l)
void
debug(int level_)
protected void
fireFileChooserEvent()
File
getCurrentDirectory()
Returns the currently displayed directory.
FileFilter
getFileFilter()
Returns the currently selected file filter.
int
getFileSelectionMode()
Returns the current file-selection mode.
int
getHeight()
Required to implement abstract method of JComponent (never used).
File
getSelectedFile()
Get the File selected by the user.
Dimension
getSize()
Required to implement abstract method of JComponent (never used).
int
getWidth()
Required to implement abstract method of JComponent (never used).
Dimension
minimumSize()
Required to implement abstract method of JComponent (never used).
void
setCurrentDirectory(File dir_)
Set the current directory.
void
setDialogTitle(String title_)
void
setFileFilter(FileFilter filter_)
Sets the current file filter.
void
setFileSelectionMode(int mode_)
Sets the JFileChooser to allow the user to select files only directories only, or files and directories.
void
setSelectedFile(File file_)
int
showDialog(Component parent_, String approveButtonText_)
Pops up a custom file chooser dialog with a custom approve button.
int
showOpenDialog(Component parent_)
Pops up a "Open File" file chooser dialog; this is a convenience method and is equivalent to showDialog(Component, "Open").
int
showSaveDialog(Component parent_)
Pops up a "Save File" file chooser dialog; this is a convenience method and is equivalent to showDialog(Component, "Save").

Methods inherited from class charvax.swing.JComponent

draw, getBorder, getInsets, setBorder

Methods inherited from class charva.awt.Component

addFocusListener, addKeyListener, contains, contains, debug, draw, getAlignmentX, getAlignmentY, getAncestorWindow, getBackground, getBounds, getCursesColor, getForeground, getHeight, getLocation, getLocationOnScreen, getName, getParent, getSize, getWidth, hasFocus, hide, invalidate, isDisplayed, isEnabled, isFocusTraversable, isTotallyObscured, isValid, isVisible, minimumSize, processEvent, processFocusEvent, processKeyEvent, processMouseEvent, repaint, requestFocus, requestSync, setBackground, setEnabled, setForeground, setLocation, setLocation, setName, setParent, setVisible, show, validate, validateCursesColor

Field Details

APPROVE_ACCELERATOR

public static int APPROVE_ACCELERATOR

APPROVE_LABEL

public static String APPROVE_LABEL

APPROVE_OPTION

public static final int APPROVE_OPTION
Field Value:
301

CANCEL_ACCELERATOR

public static int CANCEL_ACCELERATOR

CANCEL_LABEL

public static String CANCEL_LABEL

CANCEL_OPTION

public static final int CANCEL_OPTION
Field Value:
300

DIRECTORIES_ONLY

public static final int DIRECTORIES_ONLY
Field Value:
201

ERROR_OPTION

public static final int ERROR_OPTION
Field Value:
302

FILES_AND_DIRECTORIES

public static final int FILES_AND_DIRECTORIES
Field Value:
202

FILES_ONLY

public static final int FILES_ONLY
Field Value:
200

NEW_DIRECTORY_ACCELERATOR

public static int NEW_DIRECTORY_ACCELERATOR

NEW_DIRECTORY_LABEL

public static String NEW_DIRECTORY_LABEL

PARENT_DIRECTORY_ACCELERATOR

public static int PARENT_DIRECTORY_ACCELERATOR

PARENT_DIRECTORY_LABEL

public static String PARENT_DIRECTORY_LABEL

_COLS

protected static final int _COLS
Field Value:
50

_ROWS

protected static final int _ROWS
Field Value:
20

_approveButtonText

protected String _approveButtonText

_cancelWasPressed

protected boolean _cancelWasPressed

_currentDirectory

protected File _currentDirectory
The current directory shown in the dialog.

_dirList

protected charvax.swing.JFileChooser.DirList _dirList

_fileFilter

protected FileFilter _fileFilter

_fileSelectionMode

protected int _fileSelectionMode

_filelisteners

protected Vector _filelisteners

_location

protected String _location

_title

protected String _title

Constructor Details

JFileChooser

public JFileChooser()
Constructs a JFileChooser pointing to the user's home directory.

JFileChooser

public JFileChooser(File currentDirectory_)
Constructs a JFileChooser pointing to the specified directory. Passing in a null parameter causes the JFileChooser to point to the user's home directory.

JFileChooser

public JFileChooser(String currentDirectoryPath_)
Constructs a JFileChooser with the specified pathname. Passing a value of null causes the file chooser to point to the user's home directory.

Method Details

addFileChooserListener

protected void addFileChooserListener(charvax.swing.JFileChooser.FileChooserListener l)

debug

public void debug(int level_)
Overrides:
debug in interface Component

fireFileChooserEvent

protected void fireFileChooserEvent()

getCurrentDirectory

public File getCurrentDirectory()
Returns the currently displayed directory.

getFileFilter

public FileFilter getFileFilter()
Returns the currently selected file filter.

getFileSelectionMode

public int getFileSelectionMode()
Returns the current file-selection mode.
Returns:
the file-selection mode, one of the following:

  • JFileChooser.FILES_ONLY
  • JFileChooser.DIRECTORIES_ONLY
  • JFileChooser.FILES_AND_DIRECTORIES

getHeight

public int getHeight()
Required to implement abstract method of JComponent (never used).
Overrides:
getHeight in interface Component

getSelectedFile

public File getSelectedFile()
Get the File selected by the user. If the user pressed Cancel, the return value is null.

getSize

public Dimension getSize()
Required to implement abstract method of JComponent (never used).
Overrides:
getSize in interface Component

getWidth

public int getWidth()
Required to implement abstract method of JComponent (never used).
Overrides:
getWidth in interface Component

minimumSize

public Dimension minimumSize()
Required to implement abstract method of JComponent (never used).
Overrides:
minimumSize in interface Component

setCurrentDirectory

public void setCurrentDirectory(File dir_)
Set the current directory. Passing a parameter of null cause the JFileChooser to point to the user's home directory.

setDialogTitle

public void setDialogTitle(String title_)

setFileFilter

public void setFileFilter(FileFilter filter_)
Sets the current file filter. The file filter is used by the file chooser to filter out files from the user's view.

setFileSelectionMode

public void setFileSelectionMode(int mode_)
Sets the JFileChooser to allow the user to select files only directories only, or files and directories. The default is JFileChooser.FILES_ONLY.

setSelectedFile

public void setSelectedFile(File file_)

showDialog

public int showDialog(Component parent_,
                      String approveButtonText_)
Pops up a custom file chooser dialog with a custom approve button.
Parameters:
parent_ - the parent component of the dialog; can be null.
approveButtonText_ - the custom text string to display in the Approve button.
Returns:
the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION

showOpenDialog

public int showOpenDialog(Component parent_)
Pops up a "Open File" file chooser dialog; this is a convenience method and is equivalent to showDialog(Component, "Open").
Returns:
the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION

showSaveDialog

public int showSaveDialog(Component parent_)
Pops up a "Save File" file chooser dialog; this is a convenience method and is equivalent to showDialog(Component, "Save").
Returns:
the return state of the file chooser on popdown:
  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileChooser.ERROR_OPTION