Skip to main content

SwingX: Using PromptSupport

Posted by kschaefe on July 15, 2010 at 8:21 PM PDT

With the release of SwingX 1.6.1, we now have a milestone release that contains the PromptSupport code.  So, what are prompts?  They are decorations to empty text components that usually provide a clue on how to use the component.  Search fields containing a "Search" prompt, etc.  Here, I provide a couple of examples on how to use the new SwingX code as well as a runnable application below.

PromptSupport has a rather small API of only 13 methods; most of which are not needed on a daily basis.  There are two entry points for PromptSupport: setPrompt and setBackgroudPainter.  Unless one of these is called no prompt painting will be performed.  Let's discuss each of the configurable prompts options and their defaults.

Focus Behavior
This is how the prompt reacts when the prompted component gains and looses focus.
HIDE,which hides the prompt when the component gains focus. This is the default behavior.
SHOW,which shows the prompt until the user types something.
HIGHLIGHT,which highlights the prompt text as though it were auto-selected.
Font Style
Alters the font by making it bold, Italic, or bold Italic. The default is to show the font unchanged.
Foreground Color
This is the color of the text prompt. The default is to use JTextComponent.getDisabledTextColor().
Background Color
This is the background color of the text prompt. By default this is the same color the text component normally uses.
This is the text displayed for the prompt. This or a background painter is required.
Background Painter
A painter that overlays the background, but lies beneath the prompt text. The painter allows non-text prompts, gradient backgrounds, or whatever you can imagine. This or prompt is required.

A simple example, using just the defaults:

JTextField tf = new JTextField(5);

PromptSupport.setPrompt("A Prompt", tf);

Specifying a prompt with foreground and background colors:

JTextField tf = new JTextField(5);

PromptSupport.init("Custom Prompt Colors", Color.GREEN, Color.BLACK, tf);

More examples are available in the Webstart demo.  The source is available here.  As always, please provide comments and feedback on our forums and any issues to our tracker.

Related Topics >>


Linux visual glitch

There's a visual glitch on linux. On linux, in gnome: - open the webstart op - select the 2th or 3th textfield, type "hello" - the border of that textfield dissappears as soon as the "h" or more is shown

Bug Report

Can you please file a bug report at  A screenshot would be great, as well as any exception info, etc.  I don't have a linux test box handy, so any digging would be greatly appreciated.



Great stuff.... Hopefully this will get pushed to Maven Central soon...

 SwingX 1.6.1 is now in the

 SwingX 1.6.1 is now in the central repo.