/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
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"));
formBuilder.setDefaultDialogBorder();
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();
this.infoLabel.setIcon(ValidationResultViewFactory.getInfoIcon());
this.infoLabel.setVisible(false);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(new FocusChangeHandler());
formBuilder.append(this.infoLabel, 3);
formBuilder.append("Name:", nameField);
formBuilder.append("Site Url:", siteField);
formBuilder.append("Feed Url:", feedField);
add(formBuilder.getPanel());
}
private class FocusChangeHandler implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if (!"permanentFocusOwner".equals(propertyName))
return;
Component focusOwner = KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
String focusHint = (focusOwner instanceof JComponent)
? (String) ValidationComponentUtils
.getInputHint((JComponent) focusOwner)
: null;
infoLabel.setText(focusHint);
infoLabel.setVisible(focusHint != null);
}
}
public static void main(String[] a){
JFrame f = new JFrame("Info Assist Example");
f.setDefaultCloseOperation(2);
f.add(new InfoAssistExample());
f.pack();
f.setVisible(true);
}
}
|