Info Assist Example : Data Validation « Swing Components « Java

Info Assist Example

Info Assist Example
Code revised from Desktop Java Live:

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.validation.view.ValidationComponentUtils;
import com.jgoodies.validation.view.ValidationResultViewFactory;

public class InfoAssistExample extends JPanel {
    private JLabel infoLabel;

    public InfoAssistExample() {
        DefaultFormBuilder formBuilder = new DefaultFormBuilder(new FormLayout("right:pref, 3dlu, p:g"));

        JTextField nameField = new JTextField();
        JTextField feedField = new JTextField();
        JTextField siteField = new JTextField();

        ValidationComponentUtils.setInputHint(nameField, "Enter a name.");
        ValidationComponentUtils.setInputHint(feedField, "Enter a valid rss feed url.");
        ValidationComponentUtils.setInputHint(siteField, "Enter a site url.");

        this.infoLabel = new JLabel();

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(new FocusChangeHandler());

        formBuilder.append(this.infoLabel, 3);
        formBuilder.append("Name:", nameField);
        formBuilder.append("Site Url:", siteField);
        formBuilder.append("Feed Url:", feedField);


    private class FocusChangeHandler implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if (!"permanentFocusOwner".equals(propertyName))

            Component focusOwner = KeyboardFocusManager

            String focusHint = (focusOwner instanceof JComponent)
                    ? (String) ValidationComponentUtils
                    .getInputHint((JComponent) focusOwner)
                    : null;

            infoLabel.setVisible(focusHint != null);

    public static void main(String[] a){
      JFrame f = new JFrame("Info Assist Example");
      f.add(new InfoAssistExample());

       277 k)

Related examples in the same category

1.Component Hints ExampleComponent Hints Example
2.Error Dialog ExampleError Dialog Example
3.Field List Validator ExampleField List Validator Example
4.Input Verifier Example
5.Validating Domain Object ExampleValidating Domain Object Example
6.Validating On Focus Lost ExampleValidating On Focus Lost Example
7.Validating On Key Typed ExampleValidating On Key Typed Example
8.Validating Presentation Model ExampleValidating Presentation Model Example
9.Validation How to ExampleValidation How to Example
10.Validation Icons ExampleValidation Icons Example
11.Validation Results View ExampleValidation Results View Example
12.Different styles how to indicate that a component contains invalid dataDifferent styles how to indicate that a component
 contains invalid data to provide input hintshow to provide input hints
14.Different validation result viewsDifferent validation result views
15.different validation times: key-typed, focus lost, commit (OK pressed)different validation times: key-typed, focus lost, commit (OK pressed)
16.Different configurations of JFormattedTextFieldDifferent configurations of JFormattedTextField
17.different configurations of JFormattedTextField: Numberdifferent configurations of JFormattedTextField: Number
18.Different styles to mark mandatory fieldsDifferent styles to mark mandatory fields