击按钮时这些值已与 Person Bean 手动同步。
此示例的代码如下所示。
清单 1. 具有手动同步功能的示例应用程序
public class Person {
private String first;
private String last;
public Person(String first, String last) {
this.first = first;
this.last = last;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
}
public class NoBindingExample {
private Person person;
private Text firstText;
private Text lastText;
private void createControls(Shell shell) {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
Label label = new Label(shell, SWT.SHELL_TRIM);
label.setText("First:");
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
this.firstText = new Text(shell, SWT.BORDER);
this.firstText.setLayoutData(gridData);
label = new Label(shell, SWT.NONE);
label.setText("Last:");
this.lastText = new Text(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL);
this.lastText.setLayoutData(gridData);
}
private void createButtons(Shell shell) {
GridData gridData;
gridData = new GridData();
gridData.horizontalAlignment = SWT.CENTER;
gridData.horizontalSpan = 2;
Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(gridData);
button.setText("Change Name");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updatePerson();
synchronizePersonToUI();
}
});
gridData = new GridData();
gridData.horizontalAlignment = SWT.CENTER;
gridData.horizontalSpan = 2;
button = new Button(shell, SWT.PUSH);
button.setLayoutData(gridData);
button.setText("Update Person Bean From Text");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
synchronizeUIToPerson();
}
});
gridData = new GridData();
gridData.horizontalAlignment = SWT.CENTER;
gridData.horizontalSpan = 2;
button = new Button(shell, SWT.PUSH);
button.setLayoutData(gridData);
button.setText("Update Text From Person Bean");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
synchronizePersonToUI();
}
});
}
private void updatePerson() {
person.setFirst("James");
person.setLast("Gosling");
}
private void synchronizePersonToUI() {
this.firstText.setText(this.person.getFirst());
this.lastText.setText(this.person.getLast());
}
private void synchronizeUIToPerson() {
th
|