Binding JavaBean/JTextField Swing
Dans cette section, on souhaite binder le champs name d'une instance de la classe Person :
package org.akrogen.tkui.samples.xhtml.model;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("Person#setName(" + name + ")");
}
}
avec la propriété Text d'un JTextField Swing.
package org.akrogen.tkui.samples.xhtml;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.akrogen.tkui.samples.xhtml.model.Person;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.ufacekit.ui.swing.databinding.swing.SwingEventConstants;
import org.ufacekit.ui.swing.databinding.swing.SwingObservables;
import org.ufacekit.ui.swing.databinding.swing.SwingRealm;
public class JFaceDatabindingBeanTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2, 20, 20));
JTextField text1 = new JTextField();
panel.add(text1);
frame.getContentPane().add(panel);
Person person = new Person("djo");
/* ---------- Start Swing Binding ---------- */
SwingRealm.createDefault();
Realm realm = SwingObservables.getRealm();
DataBindingContext context = new DataBindingContext(realm);
// Bind JavaBean User name getter/setter (java.lang.String) with Swing JTextField.
context.bindValue(SwingObservables.observeText(text1,
SwingEventConstants.Modify), BeansObservables.observeValue(person,
"name"), null, null);
/* ---------- End Swing Binding ---------- */
frame.pack();
frame.setVisible(true);
}
}