charvax.swing
Class JFileChooser

java.lang.Object
  extended bycharva.awt.Component
      extended bycharvax.swing.JComponent
          extended bycharvax.swing.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
protected  java.lang.String _approveButtonText
           
protected  boolean _cancelWasPressed
           
protected static int _COLS
           
protected  java.io.File _currentDirectory
          The current directory shown in the dialog.
protected  charvax.swing.JFileChooser.DirList _dirList
           
protected  FileFilter _fileFilter
           
protected  java.util.Vector _filelisteners
           
protected  int _fileSelectionMode
           
protected  java.lang.String _location
           
protected static int _ROWS
           
protected  java.lang.String _title
           
static int APPROVE_ACCELERATOR
           
static java.lang.String APPROVE_LABEL
           
static int APPROVE_OPTION
           
static int CANCEL_ACCELERATOR
           
static java.lang.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 java.lang.String NEW_DIRECTORY_LABEL
           
static int PARENT_DIRECTORY_ACCELERATOR
           
static java.lang.String PARENT_DIRECTORY_LABEL
           
 
Fields inherited from class charvax.swing.JComponent
_border
 
Fields inherited from class charva.awt.Component
_alignmentX, _alignmentY, _background, _cursesColor, _enabled, _focusListeners, _foreground, _keyListeners, _origin, _parent, _visible, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JFileChooser()
          Constructs a JFileChooser pointing to the user's home directory.
JFileChooser(java.io.File currentDirectory_)
          Constructs a JFileChooser pointing to the specified directory.
JFileChooser(java.lang.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()
           
 java.io.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).
 java.io.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(java.io.File dir_)
          Set the current directory.
 void setDialogTitle(java.lang.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(java.io.File file_)
           
 int showDialog(Component parent_, java.lang.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, getAlignmentX, getAlignmentY, getAncestorWindow, getBackground, getBounds, getCursesColor, getForeground, getLocation, getLocationOnScreen, getName, getParent, hasFocus, hide, invalidate, isDisplayed, isEnabled, isFocusTraversable, isTotallyObscured, isValid, isVisible, processEvent, processFocusEvent, processKeyEvent, processMouseEvent, repaint, requestFocus, requestSync, setBackground, setBounds, setBounds, setBounds, setEnabled, setForeground, setLocation, setLocation, setName, setParent, setVisible, show, validate, validateCursesColor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_title

protected java.lang.String _title

_approveButtonText

protected java.lang.String _approveButtonText

_currentDirectory

protected java.io.File _currentDirectory
The current directory shown in the dialog.


_dirList

protected charvax.swing.JFileChooser.DirList _dirList

_location

protected java.lang.String _location

_cancelWasPressed

protected boolean _cancelWasPressed

_fileSelectionMode

protected int _fileSelectionMode

_fileFilter

protected FileFilter _fileFilter

_filelisteners

protected java.util.Vector _filelisteners

_COLS

protected static final int _COLS
See Also:
Constant Field Values

_ROWS

protected static final int _ROWS
See Also:
Constant Field Values

FILES_ONLY

public static final int FILES_ONLY
See Also:
Constant Field Values

DIRECTORIES_ONLY

public static final int DIRECTORIES_ONLY
See Also:
Constant Field Values

FILES_AND_DIRECTORIES

public static final int FILES_AND_DIRECTORIES
See Also:
Constant Field Values

CANCEL_OPTION

public static final int CANCEL_OPTION
See Also:
Constant Field Values

APPROVE_OPTION

public static final int APPROVE_OPTION
See Also:
Constant Field Values

ERROR_OPTION

public static final int ERROR_OPTION
See Also:
Constant Field Values

CANCEL_LABEL

public static java.lang.String CANCEL_LABEL

APPROVE_LABEL

public static java.lang.String APPROVE_LABEL

PARENT_DIRECTORY_LABEL

public static java.lang.String PARENT_DIRECTORY_LABEL

NEW_DIRECTORY_LABEL

public static java.lang.String NEW_DIRECTORY_LABEL

CANCEL_ACCELERATOR

public static int CANCEL_ACCELERATOR

APPROVE_ACCELERATOR

public static int APPROVE_ACCELERATOR

PARENT_DIRECTORY_ACCELERATOR

public static int PARENT_DIRECTORY_ACCELERATOR

NEW_DIRECTORY_ACCELERATOR

public static int NEW_DIRECTORY_ACCELERATOR
Constructor Detail

JFileChooser

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


JFileChooser

public JFileChooser(java.io.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(java.lang.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 Detail

setCurrentDirectory

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


getCurrentDirectory

public java.io.File getCurrentDirectory()
Returns the currently displayed directory.


getSelectedFile

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


setSelectedFile

public void setSelectedFile(java.io.File file_)

showDialog

public int showDialog(Component parent_,
                      java.lang.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

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

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

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.


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

setDialogTitle

public void setDialogTitle(java.lang.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.


getFileFilter

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


debug

public void debug(int level_)
Specified by:
debug in class Component

minimumSize

public Dimension minimumSize()
Required to implement abstract method of JComponent (never used).

Specified by:
minimumSize in class Component

getSize

public Dimension getSize()
Required to implement abstract method of JComponent (never used).

Specified by:
getSize in class Component

getHeight

public int getHeight()
Required to implement abstract method of JComponent (never used).

Specified by:
getHeight in class Component

getWidth

public int getWidth()
Required to implement abstract method of JComponent (never used).

Specified by:
getWidth in class Component

addFileChooserListener

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

fireFileChooserEvent

protected void fireFileChooserEvent()