Class TextFieldLinker
This fulfills a similar purpose to formatted text fields, except the individual parts may be
placed independent of the other components. Granted, they ought to appear in an intuitive order.
The input string is split among a collection of JTextFields each according to a given
pattern -- excluding the final field. Cursor navigation, insertion, deletion, etc. are all
applied as if the linked text fields were part of a single composite text field.
The individual text fields must be constructed and added by the user, as in the example:
Box hbox = Box.createHorizontalBox();
TextFieldLinker linker = new TextFieldLinker();
JTextField first = new JTextField();
hbox.add(first);
hbox.add(Box.createHorizontalStrut(10));
linker.linkField(first, "\\s+", " ");
JTextField second = new JTextField();
hbox.add(second);
hbox.add(new GLabel("-"));
linker.linkField(second, "-", "-");
JTextField third = new JTextField();
hbox.add(third);
linker.linkLastField(third);
linker.setVisible(true);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classThe current state of a linked field, stored separately from the actual componentprotected classA field that has been added with its corresponding separator expression and replacementprotected classA class to track the internal state gathered from the text fields -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<FocusListener> protected JTextFieldprotected final List<TextFieldLinker.LinkedField> protected AtomicIntegerprotected TextFieldLinker.LinkerState -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFocusListener(FocusListener listener) Add a focus listenerprotected JTextFieldbuildField(int i) Provides an opportunity to compose the field from an extension ofJTextFieldprotected voidCheck if this linker is mutablevoidclear()Clear the composite field, i.e., clear all the linked fieldsprotected voiddispose()Unregister all the listeners, effectively unlinking the fieldsprotected intGet the index of a field.protected voidFire the given event on all registered focus listenersgetField(int i) Get an individual field in the compositeGet the individual field last having focusintGet the number of fields in this compositegetText()Get the full composite textgetTextBeforeCursor(JTextField where) Get the text preceding the caret in the given fieldprotected voidOnce all fields are added, register all the listenersbooleanCheck if all component fields are visiblevoidlinkField(JTextField field, String exp, String sep) voidlinkField(JTextField field, Pattern pat, String sep) Add a new text field to this linkervoidlinkLastField(JTextField field) Add the final field, and actually link the fieldsvoidremoveFocusListener(FocusListener listener) Remove a focus listenervoidsetCaretPosition(int pos) Set the location of the caret among the composite textvoidSet the font for all linked fieldsvoidSet the full composite textvoidsetVisible(boolean visible) Set the visibility of all the component fieldsprotected voidSchedule a state synchronization.static TextFieldLinkerA convenient factory to build two fields separated by spaces
-
Field Details
-
linkedFields
-
lastField
-
state
-
mute
-
focusListeners
-
-
Constructor Details
-
TextFieldLinker
public TextFieldLinker()
-
-
Method Details
-
instrument
protected void instrument()Once all fields are added, register all the listeners -
dispose
protected void dispose()Unregister all the listeners, effectively unlinking the fields -
linkField
- See Also:
-
linkField
Add a new text field to this linkerLinks the given field with the others present in this linker, if any.
patis a regular expression that dictates where the given field ends, and the next field begins. Whenpatmatches a part of the text infield, the text is split and re-flowed so that the second part is moved into the next linked field. The separator is omitted from both fields. The fields should be positioned in order with labels between that display the expected separators, so that the user may understand what is happening. ThegetText()andgetTextBeforeCursor(JTextField)methods will includesepbetween the fields.Any number of fields may be added in this fashion, but the last field -- having no associated pattern or separator -- must be added using
linkLastField(JTextField). Thus, before linking is actually activated, at least one field must be present. To be meaningful, at least two fields should be linked.NOTE:
patmust acceptsep, otherwise callingsetText(String)with the results ofgetText()will have unexpected effects.- Parameters:
field- the field to linkpat- the regular expression to search for following the fieldsep- the separator that replacespatwhen matched
-
linkLastField
Add the final field, and actually link the fieldsThe fields are not effectively linked until this method is called. Additionally, once this method is called, the linker cannot take any additional fields.
- Parameters:
field- the final field
-
checkLast
protected void checkLast()Check if this linker is mutable -
findField
Get the index of a field.- Parameters:
field- the field- Returns:
- the index, or -1 if the field does not belong to this composite field
-
buildField
Provides an opportunity to compose the field from an extension ofJTextField- Parameters:
i- the index of the field to construct- Returns:
- a newly-constructed text field
-
syncStateLater
protected void syncStateLater()Schedule a state synchronization. -
clear
public void clear()Clear the composite field, i.e., clear all the linked fields -
getText
Get the full composite text- Returns:
- the text, including separators
-
setText
Set the full composite text- Parameters:
text- the text, including separators
-
setFont
Set the font for all linked fields- Parameters:
font- the new font
-
setCaretPosition
Set the location of the caret among the composite text- Parameters:
pos- the position, including separators- Throws:
BadLocationException- if the position is larger than the composite text
-
getTextBeforeCursor
Get the text preceding the caret in the given field- Parameters:
where- the field whose caret to consider- Returns:
- the text
-
getField
Get an individual field in the composite- Parameters:
i- the index of the field- Returns:
- the field
-
getFocusedField
Get the individual field last having focusEffectively, this gives the field containing the composite caret
- Returns:
- the last-focused field
-
getNumFields
public int getNumFields()Get the number of fields in this composite- Returns:
- the field count
-
setVisible
public void setVisible(boolean visible) Set the visibility of all the component fields- Parameters:
visible- true to show, false to hide
-
isVisible
public boolean isVisible()Check if all component fields are visible- Returns:
- false if any component is not visible, true otherwise
-
addFocusListener
Add a focus listenerThe focus listener will receive a callback only when focus is passed completely outside the composite text field. No events are generated when focus passes from one field in the composite to another.
- Parameters:
listener- the focus listener to add
-
removeFocusListener
Remove a focus listener- Parameters:
listener- the focus listener to remove
-
fireFocusListeners
Fire the given event on all registered focus listeners- Parameters:
ev- the event
-
twoSpacedFields
A convenient factory to build two fields separated by spaces- Returns:
- the linker containing two new linked
JTextFields
-