/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class PropertyAdapterExample1 extends JPanel {
private PersonBean personBean;
public PropertyAdapterExample1() {
DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:g"));
defaultFormBuilder.setDefaultDialogBorder();
this.personBean = new PersonBean("Scott", "Delap");
PropertyAdapter firstNameAdapter = new PropertyAdapter(this.personBean, "firstName");
PropertyAdapter lastNameAdapter = new PropertyAdapter(this.personBean, "lastName");
JTextField firstNameTextField = BasicComponentFactory.createTextField(firstNameAdapter);
JTextField lastNameTextField = BasicComponentFactory.createTextField(lastNameAdapter);
defaultFormBuilder.append("First Name: ", firstNameTextField);
defaultFormBuilder.append("Last Name: ", lastNameTextField);
defaultFormBuilder.append(new JButton(new ShowValueHolderValueAction()), 3);
add(defaultFormBuilder.getPanel());
}
public class PersonBean {
private String firstName;
private String lastName;
public PersonBean(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
private class ShowValueHolderValueAction extends AbstractAction {
public ShowValueHolderValueAction() {
super("Show Value");
}
public void actionPerformed(ActionEvent event) {
StringBuffer message = new StringBuffer();
message.append("<html>");
message.append("<b>First Name:</b> ");
message.append(personBean.getFirstName());
message.append("<br><b>Last Name:</b> ");
message.append(personBean.getLastName());
message.append("</html>");
JOptionPane.showMessageDialog(null, message.toString());
}
}
public static void main(String[] a){
JFrame f = new JFrame("Property Adapter Example 1");
f.setDefaultCloseOperation(2);
f.add(new PropertyAdapterExample1());
f.pack();
f.setVisible(true);
}
}
|