buoy.widget

Class BFileChooser


public class BFileChooser
extends Widget

A BFileChooser is a Widget that allows the user to select files or directories from the file system. It has modes for loading files, saving files, and choosing directories. It supports both single and multiple selection modes, and you can optionally restrict the list of files shown in the dialog by setting a FileFilter.

BFileChooser can be used in two different ways. First, it can be added to a container like any other Widget. This is useful when you want it to appear inside of a window along with other Widgets.

Most often, however, BFileChooser is used in a modal dialog as a self contained user interface element. To use it this way, you simply instantiate a BFileChooser, set any properties, and then call showDialog() to display it. showDialog() will automatically create a dialog, add the BFileChooser, display it, and block until the user dismisses the dialog. You can reuse a single BFileChooser by repeatedly calling showDialog().

In addition to the event types generated by all Widgets, BFileChoosers generate the following event types:

Author:
Peter Eastman

Nested Class Summary

static class
BFileChooser.SelectionMode
This inner class represents a mode for the file chooser.

Field Summary

static BFileChooser.SelectionMode
OPEN_FILE
static BFileChooser.SelectionMode
SAVE_FILE
static BFileChooser.SelectionMode
SELECT_FOLDER

Constructor Summary

BFileChooser()
Create a new BFileChooser in OPEN_FILE mode.
BFileChooser(BFileChooser.SelectionMode mode, String title)
Create a new BFileChooser
BFileChooser(BFileChooser.SelectionMode mode, String title, File directory)
Create a new BFileChooser.

Method Summary

JFileChooser
getComponent()
Get the java.awt.Component corresponding to this Widget.
File
getDirectory()
Get the directory displayed in this file chooser.
FileFilter
getFileFilter()
Get the FileFilter which restricts the list of files shown in the dialog.
BFileChooser.SelectionMode
getMode()
Get the select mode for this file chooser (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER).
File
getSelectedFile()
Get the file selected in the file chooser.
File[]
getSelectedFiles()
Get the list of selected files.
String
getTitle()
Get the title displayed on the dialog.
boolean
isMultipleSelectionEnabled()
Get whether the user is allowed to select multiple files.
void
setDirectory(File directory)
Set the directory displayed in this file chooser.
void
setFileFilter(FileFilter filter)
Set the FileFilter which restricts the list of files shown in the dialog.
void
setMode(BFileChooser.SelectionMode mode)
Set the select mode for this file chooser (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER).
void
setMultipleSelectionEnabled(boolean multiple)
Set whether the user is allowed to select multiple files.
void
setSelectedFile(File file)
Set the file selected in the file chooser.
void
setSelectedFiles(files[] )
Set the list of selected files.
void
setTitle(String title)
Set the title displayed on the dialog.
boolean
showDialog(Widget parent)
Show a dialog containing this BFileChooser and block until the user closes it.

Methods inherited from class buoy.widget.Widget

addEventLink, dispatchEvent, getBackground, getBounds, getComponent, getCursor, getFont, getMaximumSize, getMinimumSize, getName, getParent, getPreferredSize, hasFocus, isEnabled, isFocusable, isVisible, repaint, requestFocus, setBackground, setCursor, setEnabled, setFocusable, setFont, setName, setVisible

Methods inherited from class buoy.event.EventSource

addEventLink, addEventLink, addEventLink, dispatchEvent, removeEventLink

Field Details

OPEN_FILE

public static final BFileChooser.SelectionMode OPEN_FILE

SAVE_FILE

public static final BFileChooser.SelectionMode SAVE_FILE

SELECT_FOLDER

public static final BFileChooser.SelectionMode SELECT_FOLDER

Constructor Details

BFileChooser

public BFileChooser()
Create a new BFileChooser in OPEN_FILE mode.

BFileChooser

public BFileChooser(BFileChooser.SelectionMode mode,
                    String title)
Create a new BFileChooser
Parameters:
mode - the selection mode (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER)
title - the title displayed on the dialog

BFileChooser

public BFileChooser(BFileChooser.SelectionMode mode,
                    String title,
                    File directory)
Create a new BFileChooser.
Parameters:
mode - the selection mode (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER)
title - the title displayed on the dialog
directory - the directory which the file chooser should initially display

Method Details

getComponent

public JFileChooser getComponent()
Get the java.awt.Component corresponding to this Widget.
Overrides:
getComponent in interface Widget

getDirectory

public File getDirectory()
Get the directory displayed in this file chooser.

getFileFilter

public FileFilter getFileFilter()
Get the FileFilter which restricts the list of files shown in the dialog.

getMode

public BFileChooser.SelectionMode getMode()
Get the select mode for this file chooser (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER).

getSelectedFile

public File getSelectedFile()
Get the file selected in the file chooser. If multiple files are selected, no guarantees are made about which one will be returned, so you should use getSelectedFiles() instead of this method whenever multiple selection is enabled.

If no file is selected, this returns null.


getSelectedFiles

public File[] getSelectedFiles()
Get the list of selected files. This should be used instead of getSelectedFile() when multiple selection is enabled.

getTitle

public String getTitle()
Get the title displayed on the dialog.

isMultipleSelectionEnabled

public boolean isMultipleSelectionEnabled()
Get whether the user is allowed to select multiple files.

setDirectory

public void setDirectory(File directory)
Set the directory displayed in this file chooser.

setFileFilter

public void setFileFilter(FileFilter filter)
Set the FileFilter which restricts the list of files shown in the dialog.

setMode

public void setMode(BFileChooser.SelectionMode mode)
Set the select mode for this file chooser (OPEN_FILE, SAVE_FILE, or SELECT_FOLDER).

setMultipleSelectionEnabled

public void setMultipleSelectionEnabled(boolean multiple)
Set whether the user is allowed to select multiple files.

setSelectedFile

public void setSelectedFile(File file)
Set the file selected in the file chooser.

setSelectedFiles

public void setSelectedFiles(files[] )
            throws IllegalArgumentException
Set the list of selected files.

setTitle

public void setTitle(String title)
Set the title displayed on the dialog.

showDialog

public boolean showDialog(Widget parent)
Show a dialog containing this BFileChooser and block until the user closes it. After this method returns, you can call getSelectedFile() or getSelectedFiles() to determine which file or files were selected.
Parameters:
parent - the dialog's parent Widget (usually a WindowWidget). This may be null.
Returns:
true if the user clicked the accept button (usually labelled "Open" or "Save"), false if they clicked the cancel button

Written by Peter Eastman.