Class PromptSupport
- java.lang.Object
-
- org.jdesktop.swingx.prompt.PromptSupport
-
public final class PromptSupport extends java.lang.Object
Sets prompt text, foreground, background and
PromptSupport.FocusBehavior
properties on a JTextComponent by callingJComponent.putClientProperty(Object, Object)
. These properties are used byPromptTextUI
instances to render the prompt of a text component.This class is used by
JXTextField
,JXFormattedTextField
andJXTextArea
to get and set prompt properties.PromptTextUI
retrieves these properties using PromptSupport.- See Also:
JXTextField
,JXFormattedTextField
,JXTextArea
,PromptTextUI
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PromptSupport.FocusBehavior
Determines how theJTextComponent
is rendered when focused and no text is present.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BACKGROUND
The prompt background property.static java.lang.String
BACKGROUND_PAINTER
The prompt background property.static java.lang.String
FOCUS_BEHAVIOR
The focus behavior property.static java.lang.String
FONT_STYLE
The font style property, if different from the components font.static java.lang.String
FOREGROUND
The color of the prompt text property.static java.lang.String
PROMPT
The prompt text property.
-
Constructor Summary
Constructors Modifier Constructor Description private
PromptSupport()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.awt.Color
getBackground(javax.swing.text.JTextComponent textComponent)
Get the background color of thetextComponent
, when no text is present.static <T extends javax.swing.text.JTextComponent>
Painter<? super T>getBackgroundPainter(T textComponent)
Get the background painter of thetextComponent
, when no text is present.static PromptSupport.FocusBehavior
getFocusBehavior(javax.swing.text.JTextComponent textComponent)
Get thePromptSupport.FocusBehavior
oftextComponent
.static java.lang.Integer
getFontStyle(javax.swing.text.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 java.awt.Color
getForeground(javax.swing.text.JTextComponent textComponent)
Get the foreground color of the prompt text.static java.lang.String
getPrompt(javax.swing.text.JTextComponent textComponent)
Get the prompt text oftextComponent
.static void
init(java.lang.String promptText, java.awt.Color promptForeground, java.awt.Color promptBackground, javax.swing.text.JTextComponent textComponent)
Convenience method to set thepromptText
andpromptTextColor
on aJTextComponent
.static void
setBackground(java.awt.Color background, javax.swing.text.JTextComponent textComponent)
Sets the prompts background color ontextComponent
and repaints the component to reflect the changes.static <T extends javax.swing.text.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, javax.swing.text.JTextComponent textComponent)
Sets thePromptSupport.FocusBehavior
ontextComponent
and repaints the component to reflect the changes, if it is the focus owner.static void
setFontStyle(java.lang.Integer fontStyle, javax.swing.text.JTextComponent textComponent)
Set the style of the prompt font, if different from thetextComponent
s font.static void
setForeground(java.awt.Color promptTextColor, javax.swing.text.JTextComponent textComponent)
Sets the foreground color of the prompt ontextComponent
and repaints the component to reflect the changes.static void
setPrompt(java.lang.String promptText, javax.swing.text.JTextComponent textComponent)
Sets the prompt text ontextComponent
.
-
-
-
Field Detail
-
PROMPT
public static final java.lang.String PROMPT
The prompt text property.- See Also:
- Constant Field Values
-
FOREGROUND
public static final java.lang.String FOREGROUND
The color of the prompt text property.- See Also:
- Constant Field Values
-
BACKGROUND
public static final java.lang.String BACKGROUND
The prompt background property.- See Also:
- Constant Field Values
-
BACKGROUND_PAINTER
public static final java.lang.String BACKGROUND_PAINTER
The prompt background property.- See Also:
- Constant Field Values
-
FOCUS_BEHAVIOR
public static final java.lang.String FOCUS_BEHAVIOR
The focus behavior property.- See Also:
- Constant Field Values
-
FONT_STYLE
public static final java.lang.String FONT_STYLE
The font style property, if different from the components font.- See Also:
- Constant Field Values
-
-
Method Detail
-
init
public static void init(java.lang.String promptText, java.awt.Color promptForeground, java.awt.Color promptBackground, javax.swing.text.JTextComponent textComponent)
Convenience method to set the
promptText
andpromptTextColor
on aJTextComponent
.If
stayOnUIChange
is true, The prompt support will stay installed, even when the text components UI changes. See#install(JTextComponent, boolean)
.- Parameters:
promptText
-promptForeground
-promptBackground
-textComponent
-
-
getFocusBehavior
public static PromptSupport.FocusBehavior getFocusBehavior(javax.swing.text.JTextComponent textComponent)
Get thePromptSupport.FocusBehavior
oftextComponent
.- Parameters:
textComponent
-- Returns:
- the
PromptSupport.FocusBehavior
orPromptSupport.FocusBehavior.HIDE_PROMPT
if none is set
-
setFocusBehavior
public static void setFocusBehavior(PromptSupport.FocusBehavior focusBehavior, javax.swing.text.JTextComponent textComponent)
Sets thePromptSupport.FocusBehavior
ontextComponent
and repaints the component to reflect the changes, if it is the focus owner.- Parameters:
focusBehavior
-textComponent
-
-
getPrompt
public static java.lang.String getPrompt(javax.swing.text.JTextComponent textComponent)
Get the prompt text oftextComponent
.- Parameters:
textComponent
-- Returns:
- the prompt text
-
setPrompt
public static void setPrompt(java.lang.String promptText, javax.swing.text.JTextComponent textComponent)
Sets the prompt text on
textComponent
. 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
-textComponent
-
-
getForeground
public static java.awt.Color getForeground(javax.swing.text.JTextComponent textComponent)
Get the foreground color of the prompt text. If no color has been set, thetextComponent
s disabled text color will be returned.- Parameters:
textComponent
-- Returns:
- the color of the prompt text or
JTextComponent.getDisabledTextColor()
if none is set
-
setForeground
public static void setForeground(java.awt.Color promptTextColor, javax.swing.text.JTextComponent textComponent)
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
-textComponent
-
-
getBackground
public static java.awt.Color getBackground(javax.swing.text.JTextComponent textComponent)
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
-- Returns:
- the the background color of the text component, when no text is present
-
setBackground
public static void setBackground(java.awt.Color background, javax.swing.text.JTextComponent textComponent)
Sets the prompts background color on
textComponent
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
-textComponent
-
-
getBackgroundPainter
public static <T extends javax.swing.text.JTextComponent> Painter<? super T> getBackgroundPainter(T textComponent)
Get the background painter of thetextComponent
, when no text is present. If no painter has been set, thennull
will be returned.- Parameters:
textComponent
-- Returns:
- the background painter of the text component
-
setBackgroundPainter
public static <T extends javax.swing.text.JTextComponent> void setBackgroundPainter(Painter<? super T> background, T textComponent)
Sets the prompts background painter on
textComponent
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
.- Parameters:
background
-textComponent
-
-
setFontStyle
public static void setFontStyle(java.lang.Integer fontStyle, javax.swing.text.JTextComponent textComponent)
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
-textComponent
-
-
getFontStyle
public static java.lang.Integer getFontStyle(javax.swing.text.JTextComponent textComponent)
Returns the font style of the prompt text, ornull
if the prompt's font style should not differ from thetextComponent
s font.- Parameters:
textComponent
-- Returns:
- font style of the prompt text
-
-