Package ca.corbett.extras
Class TextInputDialog
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Dialog
javax.swing.JDialog
ca.corbett.extras.TextInputDialog
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible,RootPaneContainer,WindowConstants
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumprotected classListens for window close events and treats them the same way we handle "cancel".Nested classes/interfaces inherited from class javax.swing.JDialog
JDialog.AccessibleJDialogNested classes/interfaces inherited from class java.awt.Dialog
Dialog.AccessibleAWTDialog, Dialog.ModalExclusionType, Dialog.ModalityTypeNested classes/interfaces inherited from class java.awt.Window
Window.AccessibleAWTWindow, Window.TypeNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JButtonprotected FormPanelprotected final TextInputDialog.InputTypeprotected final KeyStrokeManagerprotected LongTextFieldprotected JButtonprotected Stringprotected ShortTextFieldFields inherited from class javax.swing.JDialog
accessibleContext, rootPane, rootPaneCheckingEnabledFields inherited from class java.awt.Dialog
DEFAULT_MODALITY_TYPEFields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, EXIT_ON_CLOSE, HIDE_ON_CLOSE -
Constructor Summary
ConstructorsConstructorDescriptionTextInputDialog(Window owner, String title) TextInputDialog(Window owner, String title, TextInputDialog.InputType inputType) TextInputDialog(String title) TextInputDialog(String title, TextInputDialog.InputType inputType) -
Method Summary
Modifier and TypeMethodDescriptionaddValidator(FieldValidator<? extends FormField> validator) Adds a FieldValidator to the text field, allowing whatever custom validation rules you want to enforce on the input.protected voidInvoked internally to configure keyboard shortcuts for this dialog.protected intgetCols()If InputType is SingleLine, this controls the column width of the text field.Returns the label used on the confirm button (defaults to "OK").Returns the text input by the user, or null if the user canceled.protected intgetRows()If InputType is SingleLine, this value is ignored.protected voidhandleButtonClick(boolean isOkButton) Invoked internally to close the dialog.booleanReports whether blank values are allowed in the text field.removeValidator(FieldValidator<FormField> validator) Removes the given FieldValidator from the text field.setAllowBlank(boolean allow) Decides whether blank values should be allowed in the text field.setConfirmLabel(String label) Changes the label used on the confirm button (defaults to "OK").setInitialText(String text) Sets the text that should appear in the text field when the dialog is first shown.voidsetVisible(boolean visible) static StringshowDialog(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.Methods inherited from class javax.swing.JDialog
addImpl, createRootPane, dialogInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, updateMethods inherited from class java.awt.Dialog
addNotify, getModalityType, getTitle, hide, isModal, isResizable, isUndecorated, setBackground, setModal, setModalityType, setOpacity, setResizable, setShape, setTitle, setUndecorated, show, toBackMethods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeNotify, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImage, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setType, toFrontMethods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate, validateTreeMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setMixingCutoutShape, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
inputType
-
keyStrokeManager
-
formPanel
-
shortTextField
-
longTextField
-
okButton
-
cancelButton
-
result
-
-
Constructor Details
-
TextInputDialog
public TextInputDialog() -
TextInputDialog
-
TextInputDialog
-
TextInputDialog
-
TextInputDialog
-
-
Method Details
-
setVisible
public void setVisible(boolean visible) - Overrides:
setVisiblein classDialog
-
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 dialoginputType- 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
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
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
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
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
Returns the label used on the confirm button (defaults to "OK").- Returns:
- the label used on the confirm button.
-
setConfirmLabel
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
-
setPrompt
-
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.
-