Class TextInputDialog

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer, WindowConstants

public class TextInputDialog extends JDialog
Provides an easy way to get text input from the user, very similar to JOptionPane's showInputDialog method, but with optional configurable validation on the allowed input.
Since:
swing-extras 2.8
Author:
scorbo2
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • setVisible

      public void setVisible(boolean visible)
      Overrides:
      setVisible in class Dialog
    • getResult

      public String getResult()
      Returns the text input by the user, or null if the user canceled.
      Returns:
      The user-entered text, or null if cancel was selected.
    • showDialog

      public static String showDialog(Window owner, String title, TextInputDialog.InputType inputType, boolean allowBlank, FieldValidator<? extends FormField>... validators)
      Static convenience method to show a TextInputDialog and return the result in one line of code.
      Parameters:
      owner - the parent window to center this dialog on (can be null)
      title - the title to show on the dialog
      inputType - the type of input to allow (single-line or multi-line)
      allowBlank - whether to allow blank values (true to allow, false to disallow)
      validators - optional FieldValidators to apply to the input (can be empty)
      Returns:
      the text input by the user, or null if the user canceled or closed the dialog manually.
    • setAllowBlank

      public TextInputDialog setAllowBlank(boolean allow)
      Decides whether blank values should be allowed in the text field. Blank values are allowed by default.
      Parameters:
      allow - true to allow blank values, false to disallow them.
      Returns:
      this dialog, for method chaining.
    • isAllowBlank

      public boolean isAllowBlank()
      Reports whether blank values are allowed in the text field.
      Returns:
      true if blank values are allowed, false otherwise.
    • setInitialText

      public TextInputDialog setInitialText(String text)
      Sets the text that should appear in the text field when the dialog is first shown. By default, this is blank.
      Parameters:
      text - the initial text to show in the text field when the dialog is first displayed.
      Returns:
      this dialog, for method chaining.
    • addValidator

      public TextInputDialog addValidator(FieldValidator<? extends FormField> validator)
      Adds a FieldValidator to the text field, allowing whatever custom validation rules you want to enforce on the input. You can add as many validators as you like, and they will all be applied when the user clicks the confirm button. If any validator fails, the dialog will remain open and will show validation messages to the user.

      The given validator can be typed to any subclass of FormField. This lets you use some of the built-in text validators in swing-extras, from the ca.corbett.forms.validators package, which are typed to ShortTextField or LongTextField.

      Parameters:
      validator - the FieldValidator to add to the text field.
      Returns:
      this dialog, for method chaining.
    • removeValidator

      public TextInputDialog removeValidator(FieldValidator<FormField> validator)
      Removes the given FieldValidator from the text field.
      Parameters:
      validator - the FieldValidator to remove from the text field.
      Returns:
      this dialog, for method chaining.
    • getConfirmLabel

      public String getConfirmLabel()
      Returns the label used on the confirm button (defaults to "OK").
      Returns:
      the label used on the confirm button.
    • setConfirmLabel

      public TextInputDialog setConfirmLabel(String label)
      Changes the label used on the confirm button (defaults to "OK").
      Parameters:
      label - the new label to use on the confirm button.
      Returns:
      this dialog, for method chaining.
    • getPrompt

      public String getPrompt()
    • setPrompt

      public TextInputDialog setPrompt(String prompt)
    • getCols

      protected int getCols()
      If InputType is SingleLine, this controls the column width of the text field. If InputType is MultiLine, this value is ignored. Due to the way our FormPanel is created, this is not directly configurable. However, you can extend this class and override this method if you really want a different width for the text field.
      Returns:
      the column width of the text field (defaults to 20).
    • getRows

      protected int getRows()
      If InputType is SingleLine, this value is ignored. If InputType is MultiLine, this controls the row height of the text area. Note that this merely controls the initial height. In MultiLine mode, the dialog is freely resizable, and the text area will resize as needed.
      Returns:
      the row height of the text area.
    • handleButtonClick

      protected void handleButtonClick(boolean isOkButton)
      Invoked internally to close the dialog. If the confirm button was clicked, this method first checks if the form is valid according to all validators. If the form is not valid, it will not close and will instead show validation messages to the user. If the form is valid, or if the cancel button was clicked, the dialog will close.
      Parameters:
      isOkButton - true if the confirm button was clicked, false if the cancel button was clicked.
    • configureKeyboardShortcuts

      protected void configureKeyboardShortcuts()
      Invoked internally to configure keyboard shortcuts for this dialog. By default, this registers "esc" to trigger the cancel button, and if in single-line mode, it also registers "enter" to trigger the confirm button.