Class PromptSupport
Sets prompt text, foreground, background and PromptSupport.FocusBehavior
properties
on a JTextComponent by calling
JComponent.putClientProperty(Object, Object)
. These properties
are used by PromptTextUI
instances to render the prompt of a text
component.
This class is used by JXTextField
, JXFormattedTextField
and
JXTextArea
to get and set prompt properties. PromptTextUI
retrieves these properties using PromptSupport.
- Author:
- Peter Weishapl petw@gmx.net, Karl Schaefer
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Determines how theJTextComponent
is rendered when focused and no text is present. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The prompt background property.static final String
The prompt background property.static final String
The focus behavior property.static final String
The font style property, if different from the components font.static final String
The color of the prompt text property.static final String
The prompt text property. -
Method Summary
Modifier and TypeMethodDescriptionstatic Color
getBackground
(JTextComponent textComponent) Get the background color of thetextComponent
, when no text is present.static <T extends JTextComponent>
Painter<? super T>getBackgroundPainter
(T textComponent) Get the background painter of thetextComponent
, when no text is present.static PromptSupport.FocusBehavior
getFocusBehavior
(JTextComponent textComponent) Get thePromptSupport.FocusBehavior
oftextComponent
.static Integer
getFontStyle
(JTextComponent textComponent) Returns the font style of the prompt text, ornull
if the prompt's font style should not differ from thetextComponent
s font.static Color
getForeground
(JTextComponent textComponent) Get the foreground color of the prompt text.static String
getPrompt
(JTextComponent textComponent) Get the prompt text oftextComponent
.static void
init
(String promptText, Color promptForeground, Color promptBackground, JTextComponent textComponent) static void
setBackground
(Color background, JTextComponent textComponent) Sets the prompts background color ontextComponent
and repaints the component to reflect the changes.static <T extends JTextComponent>
voidsetBackgroundPainter
(Painter<? super T> background, T textComponent) Sets the prompts background painter ontextComponent
and repaints the component to reflect the changes.static void
setFocusBehavior
(PromptSupport.FocusBehavior focusBehavior, JTextComponent textComponent) Sets thePromptSupport.FocusBehavior
ontextComponent
and repaints the component to reflect the changes, if it is the focus owner.static void
setFontStyle
(Integer fontStyle, JTextComponent textComponent) Set the style of the prompt font, if different from thetextComponent
s font.static void
setForeground
(Color promptTextColor, JTextComponent textComponent) Sets the foreground color of the prompt ontextComponent
and repaints the component to reflect the changes.static void
setPrompt
(String promptText, JTextComponent textComponent) Sets the prompt text ontextComponent
.
-
Field Details
-
PROMPT
The prompt text property.- See Also:
-
FOREGROUND
The color of the prompt text property.- See Also:
-
BACKGROUND
The prompt background property.- See Also:
-
BACKGROUND_PAINTER
The prompt background property.- See Also:
-
FOCUS_BEHAVIOR
The focus behavior property.- See Also:
-
FONT_STYLE
The font style property, if different from the components font.- See Also:
-
-
Method Details
-
init
public static void init(String promptText, Color promptForeground, Color promptBackground, JTextComponent textComponent) Convenience method to set thepromptText
andpromptTextColor
on aJTextComponent
.If
stayOnUIChange
is true, The prompt support will stay installed, even when the text components UI changes.- Parameters:
promptText
- StringpromptForeground
- ColorpromptBackground
- ColortextComponent
- JTextComponent
-
getFocusBehavior
Get thePromptSupport.FocusBehavior
oftextComponent
.- Parameters:
textComponent
- JTextComponent- Returns:
- the
PromptSupport.FocusBehavior
orPromptSupport.FocusBehavior.HIDE_PROMPT
if none is set
-
setFocusBehavior
public static void setFocusBehavior(PromptSupport.FocusBehavior focusBehavior, JTextComponent textComponent) Sets thePromptSupport.FocusBehavior
ontextComponent
and repaints the component to reflect the changes, if it is the focus owner.- Parameters:
focusBehavior
- FocusBehaviortextComponent
- JTextComponent
-
getPrompt
Get the prompt text oftextComponent
.- Parameters:
textComponent
- JTextComponent- Returns:
- the prompt text
-
setPrompt
Sets the prompt text ontextComponent
. Also sets the tooltip text to the prompt text iftextComponent
has no tooltip text or the current tooltip text is the same as the current prompt text.Calls
install(JTextComponent)
to ensure that thetextComponent
s UI is wrapped by the appropriatePromptTextUI
.- Parameters:
promptText
- StringtextComponent
- JTextComponent
-
getForeground
Get the foreground color of the prompt text. If no color has been set, thetextComponent
s disabled text color will be returned.- Parameters:
textComponent
- JTextComponent- Returns:
- the color of the prompt text or
JTextComponent.getDisabledTextColor()
if none is set
-
setForeground
Sets the foreground color of the prompt ontextComponent
and repaints the component to reflect the changes. This color will be used when no text is present.- Parameters:
promptTextColor
- ColortextComponent
- JTextComponent
-
getBackground
Get the background color of thetextComponent
, when no text is present. If no color has been set, thetextComponent
s background color color will be returned.- Parameters:
textComponent
- JTextComponent- Returns:
- the the background color of the text component, when no text is present
-
setBackground
Sets the prompts background color ontextComponent
and repaints the component to reflect the changes. This background color will only be used when no text is present.Calls
install(JTextComponent)
to ensure that thetextComponent
s UI is wrapped by the appropriatePromptTextUI
.- Parameters:
background
- ColortextComponent
- JTextComponent
-
getBackgroundPainter
Get the background painter of thetextComponent
, when no text is present. If no painter has been set, thennull
will be returned.- Type Parameters:
T
- subtype of JTextComponent- Parameters:
textComponent
- of type T- Returns:
- the background painter of the text component
-
setBackgroundPainter
public static <T extends JTextComponent> void setBackgroundPainter(Painter<? super T> background, T textComponent) Sets the prompts background painter ontextComponent
and repaints the component to reflect the changes. This background painter will only be used when no text is present.Calls
install(JTextComponent)
to ensure that thetextComponent
s UI is wrapped by the appropriatePromptTextUI
.- Type Parameters:
T
- subtype of JTextComponent- Parameters:
background
- PaintertextComponent
- of type T
-
setFontStyle
Set the style of the prompt font, if different from the
textComponent
s font.Allowed values are
Font.PLAIN
,Font.ITALIC
,Font.BOLD
, a combination ofFont.BOLD
andFont.ITALIC
ornull
if the prompt font should be the same as thetextComponent
s font.- Parameters:
fontStyle
- IntegertextComponent
- JTextComponent
-
getFontStyle
Returns the font style of the prompt text, ornull
if the prompt's font style should not differ from thetextComponent
s font.- Parameters:
textComponent
- JTextComponent- Returns:
- font style of the prompt text
-