Class FileField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.FileField

public final class FileField extends FormField
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
  • Constructor Details

    • FileField

      public FileField(String label, File initialValue, int cols, FileField.SelectionType selectionType)
      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

      public FileField setSelectionType(FileField.SelectionType selectionType)
      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

      public FileField setSelectionType(FileField.SelectionType selectionType, boolean allowBlankValues)
      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

      public FileField.SelectionType 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

      public FileField setAllowBlankValues(boolean allow)
      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:
      setEnabled in class FormField
      Parameters:
      enabled - whether to enable or disable the components.
    • getTextField

      public JTextField getTextField()
      Allows direct access to the underlying JTextField.
    • getFile

      public File getFile()
      Returns the currently selected File from this field.
      Returns:
      A File object.
    • setFile

      public FileField setFile(File file)
      Sets the currently selected File for this field.
      Parameters:
      file - The File to select.
    • setFileFilter

      public FileField setFileFilter(FileFilter filter)
      Sets a FileFilter to use with the JFileChooser.
      Parameters:
      filter - An optional FileFilter to apply.
    • getFileFilter

      public FileFilter getFileFilter()
    • preRender

      public void preRender(JPanel container)
      Description copied from class: FormField
      Invoke 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.
      Overrides:
      preRender in class FormField
    • addFieldValidator

      public FormField addFieldValidator(FieldValidator<? extends FormField> validator)
      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:
      addFieldValidator in class FormField
    • removeFieldValidator

      public void removeFieldValidator(FieldValidator<FormField> validator)
      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:
      removeFieldValidator in class FormField
    • 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:
      removeAllFieldValidators in class FormField