Class PromptSupport

java.lang.Object
org.jdesktop.swingx.prompt.PromptSupport

public final class PromptSupport extends Object

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:
  • Field Details

  • Method Details

    • init

      public static void init(String promptText, Color promptForeground, Color promptBackground, JTextComponent textComponent)
      Convenience method to set the promptText and promptTextColor on a JTextComponent.

      If stayOnUIChange is true, The prompt support will stay installed, even when the text components UI changes.

      Parameters:
      promptText - String
      promptForeground - Color
      promptBackground - Color
      textComponent - JTextComponent
    • getFocusBehavior

      public static PromptSupport.FocusBehavior getFocusBehavior(JTextComponent textComponent)
      Get the PromptSupport.FocusBehavior of textComponent.
      Parameters:
      textComponent - JTextComponent
      Returns:
      the PromptSupport.FocusBehavior or PromptSupport.FocusBehavior.HIDE_PROMPT if none is set
    • setFocusBehavior

      public static void setFocusBehavior(PromptSupport.FocusBehavior focusBehavior, JTextComponent textComponent)
      Sets the PromptSupport.FocusBehavior on textComponent and repaints the component to reflect the changes, if it is the focus owner.
      Parameters:
      focusBehavior - FocusBehavior
      textComponent - JTextComponent
    • getPrompt

      public static String getPrompt(JTextComponent textComponent)
      Get the prompt text of textComponent.
      Parameters:
      textComponent - JTextComponent
      Returns:
      the prompt text
    • setPrompt

      public static void setPrompt(String promptText, JTextComponent textComponent)
      Sets the prompt text on textComponent. Also sets the tooltip text to the prompt text if textComponent has no tooltip text or the current tooltip text is the same as the current prompt text.

      Calls install(JTextComponent) to ensure that the textComponents UI is wrapped by the appropriate PromptTextUI.

      Parameters:
      promptText - String
      textComponent - JTextComponent
    • getForeground

      public static Color getForeground(JTextComponent textComponent)
      Get the foreground color of the prompt text. If no color has been set, the textComponents disabled text color will be returned.
      Parameters:
      textComponent - JTextComponent
      Returns:
      the color of the prompt text or JTextComponent.getDisabledTextColor() if none is set
    • setForeground

      public static void setForeground(Color promptTextColor, JTextComponent textComponent)
      Sets the foreground color of the prompt on textComponent and repaints the component to reflect the changes. This color will be used when no text is present.
      Parameters:
      promptTextColor - Color
      textComponent - JTextComponent
    • getBackground

      public static Color getBackground(JTextComponent textComponent)
      Get the background color of the textComponent, when no text is present. If no color has been set, the textComponents background color color will be returned.
      Parameters:
      textComponent - JTextComponent
      Returns:
      the the background color of the text component, when no text is present
    • setBackground

      public static void setBackground(Color background, 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 the textComponents UI is wrapped by the appropriate PromptTextUI.

      Parameters:
      background - Color
      textComponent - JTextComponent
    • getBackgroundPainter

      public static <T extends JTextComponent> Painter<? super T> getBackgroundPainter(T textComponent)
      Get the background painter of the textComponent, when no text is present. If no painter has been set, then null 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 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 the textComponents UI is wrapped by the appropriate PromptTextUI.

      Type Parameters:
      T - subtype of JTextComponent
      Parameters:
      background - Painter
      textComponent - of type T
    • setFontStyle

      public static void setFontStyle(Integer fontStyle, JTextComponent textComponent)

      Set the style of the prompt font, if different from the textComponents font.

      Allowed values are Font.PLAIN, Font.ITALIC, Font.BOLD, a combination of Font.BOLD and Font.ITALIC or null if the prompt font should be the same as the textComponents font.

      Parameters:
      fontStyle - Integer
      textComponent - JTextComponent
    • getFontStyle

      public static Integer getFontStyle(JTextComponent textComponent)
      Returns the font style of the prompt text, or null if the prompt's font style should not differ from the textComponents font.
      Parameters:
      textComponent - JTextComponent
      Returns:
      font style of the prompt text