📄 jfacedatabinding6.java
字号:
package cn.com.chengang.databinding;
import java.util.ArrayList;
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.value.WritableValue;
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.RowData;
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.Label;
import org.eclipse.swt.widgets.Shell;
public class JFaceDataBinding6 {
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();// 数据
ArrayList<String> interests = new ArrayList<String>(3);
interests.add("阅读");
interests.add("旅行");
interests.add("运动");
bean.setInterests(interests);
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);
Label label = new Label(shell, SWT.BORDER);
label.setLayoutData(new RowData(100, -1));
// 数据绑定
DataBindingContext ctx = DataBindingContextFactory.createContext(shell);
ctx.bind(new Property(combo, SWTProperties.ITEMS), new Property(bean, "interests", String.class, true), null);
WritableValue selectedItem = new WritableValue(String.class);
selectedItem.setValue("旅行");// 设置初选值
ctx.bind(new Property(combo, SWTProperties.SELECTION), selectedItem, null);
ctx.bind(label, selectedItem, null);
// ctx.bind(label, new Property(combo, SWTProperties.SELECTION), 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 + -