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 JTextField
s 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
Modifier and TypeClassDescriptionprotected class
The current state of a linked field, stored separately from the actual componentprotected class
A field that has been added with its corresponding separator expression and replacementprotected class
A class to track the internal state gathered from the text fields -
Field Summary
Modifier and TypeFieldDescriptionprotected final List
<FocusListener> protected JTextField
protected final List
<TextFieldLinker.LinkedField> protected AtomicInteger
protected TextFieldLinker.LinkerState
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addFocusListener
(FocusListener listener) Add a focus listenerprotected JTextField
buildField
(int i) Provides an opportunity to compose the field from an extension ofJTextField
protected void
Check if this linker is mutablevoid
clear()
Clear the composite field, i.e., clear all the linked fieldsprotected void
dispose()
Unregister all the listeners, effectively unlinking the fieldsprotected int
Get the index of a field.protected void
Fire the given event on all registered focus listenersgetField
(int i) Get an individual field in the compositeGet the individual field last having focusint
Get the number of fields in this compositegetText()
Get the full composite textgetTextBeforeCursor
(JTextField where) Get the text preceding the caret in the given fieldprotected void
Once all fields are added, register all the listenersboolean
Check if all component fields are visiblevoid
linkField
(JTextField field, String exp, String sep) void
linkField
(JTextField field, Pattern pat, String sep) Add a new text field to this linkervoid
linkLastField
(JTextField field) Add the final field, and actually link the fieldsvoid
removeFocusListener
(FocusListener listener) Remove a focus listenervoid
setCaretPosition
(int pos) Set the location of the caret among the composite textvoid
Set the font for all linked fieldsvoid
Set the full composite textvoid
setVisible
(boolean visible) Set the visibility of all the component fieldsprotected void
Schedule a state synchronization.static TextFieldLinker
A 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.
pat
is a regular expression that dictates where the given field ends, and the next field begins. Whenpat
matches 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 includesep
between 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:
pat
must 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 replacespat
when 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
JTextField
s
-