了解 Eclipse 中的 JFace 数据绑定,第 2 部分: 绑定的基础知识
2009-12-14 00:00:00 来源:WEB开发网单击 Eclipse 运行按钮旁边的箭头,然后选择 NoBinding 运行目标。系统将显示一个类似图 4 所示的窗口。
图 4. 运行示例
此时,用应用程序执行一些练习十分有帮助:
请注意,任何一个文本框中都没有显示文本。单击 Change Name 以将文本更改为 James Gosling。
将 First 和 Last 名称字段更改为选定的任意文本。
单击 Update Text From Person Bean。文本将恢复为 James Gosling。产生这个结果的原因是所做的字段更改并未与 Person Bean 进行同步。
重新更改文本,然后单击 Update Person Bean From Text。
重新更改文本,然后单击 Update Text from Person Bean。文本将更改回第一次输入的文本,因为在单击按钮时这些值已与 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() {
this.person.setFirst(this.firstText.getText());
this.person.setLast(this.lastText.getText());
}
public static void main(String[] args) {
NoBindingExample example = new NoBindingExample();
example.run();
}
public void run() {
this.person = new Person("Larry", "Wall");
Display display = new Display();
Shell shell = new Shell(display);
createControls(shell);
createButtons(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
- ››Eclipse 3.7反编译插件的安装
- ››eclipse CDT NDK环境搭建步骤
- ››Eclipse 如何自定义java class注释
- ››eclipse.ini内存设置
- ››Eclipse+PyDev离线配置Python开发环境
- ››Eclipse下jQuery文件报错解决方案
- ››Eclipse快捷键与使用技巧
- ››Eclipse 常用快捷键 常用技巧My Eclipse常用快捷键...
- ››Eclipse快捷键二
- ››Eclipse快捷键一
- ››Eclipse+SVN+Google Code配置过程
- ››eclipse中开发android程序时,打开layout配置文件自...
更多精彩
赞助商链接