Package ca.corbett.forms.fields
Class LabelField
java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.LabelField
Represents a form field that just presents a static label without any
user interaction. This can either be presented as a label:text pair,
like a non-editable form field, or as a single label that spans the width
of the form, like a section header. Convenience methods are exposed here
so you can control the font and colour of the label text. (You could also
do this by invoking getFieldComponent() from the FormField parent class
and casting it to a JLabel, but the convenience methods here are easier).
To reduce confusion, the terminology is as follows: (fieldLabel):(labelText),
where (fieldLabel) is optional (if not set, the colon separator will
also be hidden).
Hyperlinked labels are supported by using the setHyperlink() method and providing some Action to be invoked when the link is clicked. This will change the color of the label to blue and change the mouse pointer when the cursor is over the label, to indicate that the label is clickable.
Note that label fields ignore validation, as there's no user input to validate.
You can make a multiline label by wrapping the label text in html tags:
labelField.setText("<html>Hello<br>second line<br>another line</html>");
Hyperlinks
Hyperlinked labels are supported by using the setHyperlink() method and providing some Action to be invoked when the link is clicked. This will change the color of the label to blue and change the mouse pointer when the cursor is over the label, to indicate that the label is clickable.
- Since:
- 2019-11-26
- Author:
- scorbo2
-
Field Summary
Fields inherited from class ca.corbett.forms.fields.FormField
DEFAULT_FONT, defaultFont, extraAttributes, fieldComponent, fieldLabel, fieldValidators, helpLabel, identifier, isEnabled, isVisible, margins, validationLabel, valueChangedListeners -
Constructor Summary
ConstructorsConstructorDescriptionLabelField(String labelText) Creates a form-width label in the format "labelText".LabelField(String fieldLabelText, String labelText) Creates a label field in the format "fieldLabel:labelText". -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves any previously set hyperlink, if any.static LabelFieldcreateBoldHeaderLabel(String text) A static convenience factory method to create a bold header label with sensible defaults for a section header label.static LabelFieldcreateBoldHeaderLabel(String text, int fontSize) static LabelFieldcreateHeaderLabel(String text, Font font, int topMargin, int bottomMargin) A static convenience factory method to create a header label with the specified font and margin properties.static LabelFieldcreatePlainHeaderLabel(String text) A static convenience factory method to create a "normal" header label with sensible defaults for a form label.static LabelFieldcreatePlainHeaderLabel(String text, int fontSize) A static convenience factory method to create a "normal" header label the given font size and with top and bottom margin values read from our current margin properties.getColor()static Fontstatic Fontstatic intstatic intstatic intstatic intgetFont()getText()Returns the current label text.booleanOverridden here as we generally don't want to show a validation label on a label.booleanReports whether this is a "header" label.booleanReports whether this label has been hyperlinked (see setHyperlink).Sets the colour for the label text.Sets the font to use for the label text.static voidsetHeaderLabelExtraMargins(int normalTop, int normalBottom, int headerTop, int headerBottom) Sets the extra top and bottom margin values that we use in this class when create*HeaderLabel convenience methods are invoked.setHyperlink(Action action) Converts the field label (if present) or the header label (if isHeaderLabel()) into a hyperlink, by adding a custom mouse cursor and mouse listener with the given ActionListener attached to the single click event.Sets the label text.Methods inherited from class ca.corbett.forms.fields.FormField
addAllExtraAttributes, addFieldValidator, addValueChangedListener, clearExtraAttribute, clearExtraAttributes, clearValidationResults, equals, fireValueChangedEvent, getDefaultFont, getExtraAttribute, getFieldComponent, getFieldLabel, getHelpLabel, getHelpText, getIdentifier, getMargins, getValidationLabel, hasFieldLabel, hashCode, hasHelpLabel, isEnabled, isMultiLine, isValid, isVisible, preRender, removeAllFieldValidators, removeFieldValidator, removeValueChangedListener, setAllExtraAttributes, setDefaultFont, setEnabled, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, shouldExpand, validate
-
Constructor Details
-
LabelField
Creates a form-width label in the format "labelText".- Parameters:
labelText- The text of the label.
-
LabelField
Creates a label field in the format "fieldLabel:labelText". If fieldLabel is null or blank, only "labelText" will be displayed.- Parameters:
fieldLabelText- The optional string to show as a prefix. Can be null or blank to omit.labelText- The text of the actual label.
-
-
Method Details
-
hasValidationLabel
public boolean hasValidationLabel()Overridden here as we generally don't want to show a validation label on a label. Will return true only if one or more FieldValidators have been explicitly assigned.- Overrides:
hasValidationLabelin classFormField
-
createBoldHeaderLabel
A static convenience factory method to create a bold header label with sensible defaults for a section header label. The default values are 16 point bold black text with a slightly larger top margin and normal bottom margin.- Parameters:
text- The label text- Returns:
- A LabelField suitable for use as a header.
-
createBoldHeaderLabel
-
createPlainHeaderLabel
A static convenience factory method to create a "normal" header label with sensible defaults for a form label. The default values are 12 point plain black text with top and bottom margin values read from our current margin properties.You can control the extra top and bottom margin for generated header labels by invoking LabelField.setHeaderLabelExtraMargins() before invoking this method.
- Parameters:
text- The label text- Returns:
- A LabelField suitable for use as a regular header label.
-
createPlainHeaderLabel
A static convenience factory method to create a "normal" header label the given font size and with top and bottom margin values read from our current margin properties.You can control the extra top and bottom margin for generated header labels by invoking LabelField.setHeaderLabelExtraMargins() before invoking this method.
- Parameters:
text- The label text- Returns:
- A LabelField suitable for use as a regular header label.
-
setHeaderLabelExtraMargins
public static void setHeaderLabelExtraMargins(int normalTop, int normalBottom, int headerTop, int headerBottom) Sets the extra top and bottom margin values that we use in this class when create*HeaderLabel convenience methods are invoked.- Parameters:
normalTop- An extra pixel margin to apply above non-bolded header labels.normalBottom- An extra pixel margin to apply below non-bolded header labels.headerTop- An extra pixel margin to apply above bold header labels.headerBottom- An extra pixel margin to apply below bold header labels.
-
getExtraTopMarginNormal
public static int getExtraTopMarginNormal() -
getExtraBottomMarginNormal
public static int getExtraBottomMarginNormal() -
getExtraTopMarginHeader
public static int getExtraTopMarginHeader() -
getExtraBottomMarginHeader
public static int getExtraBottomMarginHeader() -
getDefaultHeaderFont
-
getDefaultLabelFont
-
createHeaderLabel
A static convenience factory method to create a header label with the specified font and margin properties.- Parameters:
text- The text to display.font- The font.topMargin- The top margin (default is 4).bottomMargin- The bottom margin (default is 4).- Returns:
- A header label with the specified properties.
-
isHeaderLabel
public boolean isHeaderLabel()Reports whether this is a "header" label. That means the fieldLabel text is blank or empty, so instead of a fieldLabel:labeltext pairing, we just have a formwidth-spanning labeltext instead.- Returns:
- true if this is a single label instead of this:that style.
-
isHyperlinked
public boolean isHyperlinked()Reports whether this label has been hyperlinked (see setHyperlink).- Returns:
- true if this label contains a hyperlink.
-
setHyperlink
Converts the field label (if present) or the header label (if isHeaderLabel()) into a hyperlink, by adding a custom mouse cursor and mouse listener with the given ActionListener attached to the single click event. The label font is also modified with color and underline and custom mouse cursor as hints that it is now clickable.- Parameters:
action- The Action to fire when the label is clicked.
-
clearHyperlink
public void clearHyperlink()Removes any previously set hyperlink, if any. This will return the label to DEFAULT_FONT and default label text color. -
getText
Returns the current label text.- Returns:
- The text of the label.
-
setText
Sets the label text.- Parameters:
text- The new label text.
-
setFont
Sets the font to use for the label text. This is shorthand for ((JLabel)getFieldComponent()).setFont()- Parameters:
font- The new Font to use.
-
getFont
-
setColor
Sets the colour for the label text. This is shorthand for ((JLabel)getFieldComponent()).setForeground();- Parameters:
c- The new text colour.
-
getColor
-