📄 jfacedatabinding7.java
字号:
package cn.com.chengang.databinding;
import java.util.Arrays;
import org.eclipse.jface.internal.databinding.provisional.DataBindingContext;
import org.eclipse.jface.internal.databinding.provisional.description.Property;
import org.eclipse.jface.internal.databinding.provisional.observable.list.WritableList;
import org.eclipse.jface.internal.databinding.provisional.swt.SWTProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class JFaceDataBinding7 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
shell.setLayout(new RowLayout());
final People bean = new People();// 数据
Button button = new Button(shell, SWT.NONE);
button.setText("打印数据");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("---------------------------------");
System.out.println("name=" + bean.getName());
System.out.println("age=" + bean.getAge());
System.out.println("sex=" + bean.isSex());
System.out.println("interests=" + Arrays.toString(bean.getInterests().toArray()));
}
});
// 界面组件
Combo combo = new Combo(shell, SWT.BORDER);
// 绑定初始数据
DataBindingContext ctx = DataBindingContextFactory.createContext(shell);
WritableList ageList = new WritableList(String.class);
ageList.add("0");
ageList.add("10");
ageList.add("20");
ageList.add("30");
ctx.bind(new Property(combo, SWTProperties.ITEMS), ageList, null);
ageList.add("100"); // 以后还可以再加进新项,将实现反映到界面上
combo.remove("100");// 如果从界面删除某项,ageList也会跟着删除该项。
// 初选绑定
ctx.bind(new Property(combo, SWTProperties.SELECTION), new Property(bean, "age"), null);
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -