Package ca.corbett.forms.fields
Class FileField
java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.FileField
A FormField for choosing a single directory or file.
For directories, the chosen directory must exist.
For files, you can specify whether to browse for files that must exist,
or for files that must NOT exist (for example, for a save dialog).
The underlying JFileChooser is not directly exposed, but there are some convenience methods here, like setFileFilter(), that can be used to customize it.
- Since:
- 2019-11-24
- Author:
- scorbo2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumCurrently supported selection modes for this field. -
Field Summary
Fields inherited from class ca.corbett.forms.fields.FormField
DEFAULT_FONT, defaultFont, extraAttributes, fieldComponent, fieldLabel, fieldValidators, helpLabel, identifier, isEnabled, isVisible, margins, validationLabel, valueChangedListeners -
Constructor Summary
ConstructorsConstructorDescriptionFileField(String label, File initialValue, int cols, FileField.SelectionType selectionType) Creates a FileField with the given parameters.FileField(String label, File initialValue, int cols, FileField.SelectionType selectionType, boolean allowBlank) Creates a FileField with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionaddFieldValidator(FieldValidator<? extends FormField> validator) Overridden so that it acts only on our "user-added" validators list.getFile()Returns the currently selected File from this field.Returns the SelectionType of this FileField.Allows direct access to the underlying JTextField.booleanReturns whether or not blank values are permitted in this field.voidInvoke before rendering this FormField to a container, in case the FormField needs to do some initialization specific to its new container (for example, matching the container's background color or using the container as a parent component for a popup dialog).voidOverridden so that it acts only on our "user-added" validators list.voidremoveFieldValidator(FieldValidator<FormField> validator) Overridden so that it acts only on our "user-added" validators list.setAllowBlankValues(boolean allow) Specifies whether blank values should be allowed in the text field.voidsetEnabled(boolean enabled) Overridden so we can enable/disable our choose button also.Sets the currently selected File for this field.setFileFilter(FileFilter filter) Sets a FileFilter to use with the JFileChooser.setSelectionType(FileField.SelectionType selectionType) Sets the SelectionType for this FieldField.setSelectionType(FileField.SelectionType selectionType, boolean allowBlankValues) Sets the SelectionType for this FileField.Methods inherited from class ca.corbett.forms.fields.FormField
addAllExtraAttributes, addValueChangedListener, clearExtraAttribute, clearExtraAttributes, clearValidationResults, equals, fireValueChangedEvent, getDefaultFont, getExtraAttribute, getFieldComponent, getFieldLabel, getHelpLabel, getHelpText, getIdentifier, getMargins, getValidationLabel, hasFieldLabel, hashCode, hasHelpLabel, hasValidationLabel, isEnabled, isMultiLine, isValid, isVisible, removeValueChangedListener, setAllExtraAttributes, setDefaultFont, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, shouldExpand, validate
-
Constructor Details
-
FileField
Creates a FileField with the given parameters.- Parameters:
label- The label to use for this field.initialValue- The initial File to display in the field.cols- The number of columns to use for the text box.selectionType- See SelectionType for details.
-
FileField
public FileField(String label, File initialValue, int cols, FileField.SelectionType selectionType, boolean allowBlank) Creates a FileField with the given parameters.- Parameters:
label- The label to use for this field.initialValue- The initial File to display in the field.cols- The number of columns to use for the text box.selectionType- See SelectionType for details.allowBlank- whether to allow blank values in the field.
-
-
Method Details
-
setSelectionType
Sets the SelectionType for this FieldField.- ExistingDirectory: You can browse for a directory, which must exist and be readable/writable.
- ExistingFile: You can browse for a single file, which must exist and be readable/writable.
- NonExistingFile: You can browse for a single file, which must not exist (eg. for a save dialog).
- Parameters:
selectionType- A SelectionType value as explained above.
-
setSelectionType
Sets the SelectionType for this FileField.- ExistingDirectory: You can browse for a directory, which must exist and be readable/writable.
- ExistingFile: You can browse for a single file, which must exist and be readable/writable.
- NonExistingFile: You can browse for a single file, which must not exist.
- Parameters:
selectionType- A SelectionType value as explained above.allowBlankValues- If false, the text field cannot be blanked out (i.e. no file specified at all).
-
getSelectionType
Returns the SelectionType of this FileField.- Returns:
- The current SelectionType of this field. Use setSelectionType to modify.
-
isAllowBlankValues
public boolean isAllowBlankValues()Returns whether or not blank values are permitted in this field. If true, and no value is specified in the text field, then getFile() will return null. If false, then the field will throw a validation error if no value is specified.- Returns:
- Whether or not this field considers a blank value to be valid.
-
setAllowBlankValues
Specifies whether blank values should be allowed in the text field. If false, a non-blank validator will be added automatically. -
setEnabled
public void setEnabled(boolean enabled) Overridden so we can enable/disable our choose button also.- Overrides:
setEnabledin classFormField- Parameters:
enabled- whether to enable or disable the components.
-
getTextField
Allows direct access to the underlying JTextField. -
getFile
Returns the currently selected File from this field.- Returns:
- A File object.
-
setFile
Sets the currently selected File for this field.- Parameters:
file- The File to select.
-
setFileFilter
Sets a FileFilter to use with the JFileChooser.- Parameters:
filter- An optional FileFilter to apply.
-
getFileFilter
-
preRender
Description copied from class:FormFieldInvoke before rendering this FormField to a container, in case the FormField needs to do some initialization specific to its new container (for example, matching the container's background color or using the container as a parent component for a popup dialog). The default implementation here does nothing. Overriding this method is optional. -
addFieldValidator
Overridden so that it acts only on our "user-added" validators list. We need to keep these separate from the validators that we add internally depending on selectionType.- Overrides:
addFieldValidatorin classFormField
-
removeFieldValidator
Overridden so that it acts only on our "user-added" validators list. We need to keep these separate from the validators that we add internally depending on selectionType.- Overrides:
removeFieldValidatorin classFormField
-
removeAllFieldValidators
public void removeAllFieldValidators()Overridden so that it acts only on our "user-added" validators list. We need to keep these separate from the validators that we add internally depending on selectionType.- Overrides:
removeAllFieldValidatorsin classFormField
-