⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfacedatabinding2.java

📁 SWTJFace篇项目源程序该项目包含 包含了Eclipse下构建swt的基本工程
💻 JAVA
字号:
package cn.com.chengang.databinding;

import java.util.Arrays;

import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jface.internal.databinding.provisional.BindSpec;
import org.eclipse.jface.internal.databinding.provisional.DataBindingContext;
import org.eclipse.jface.internal.databinding.provisional.description.Property;
import org.eclipse.jface.internal.databinding.provisional.validation.IValidator;
import org.eclipse.jface.internal.databinding.provisional.validation.ValidationError;
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.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class JFaceDataBinding2 {
	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()));
				System.out.println("cities=" + Arrays.toString(bean.getCities().toArray()));
			}
		});

		// 界面组件
		Text ageText = new Text(shell, SWT.BORDER);
		ageText.setLayoutData(new RowData(50, -1));
		// 数据绑定
		DataBindingContext ctx = DataBindingContextFactory.createContext(shell);
		BindSpec spec = new BindSpec(null, null, new MyValidator(), null);
		ctx.bind(ageText, new Property(bean, "age"), spec);

		// -----------------END------------------------
		shell.layout();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private static class MyValidator implements IValidator {
		// 文本框每次输入字符都将执行此方法
		public ValidationError isPartiallyValid(Object value) {
			System.out.println("isPartiallyValid=" + value);
			if (NumberUtils.isNumber((String) value))
				return null;
			else
				return new ValidationError(ValidationError.ERROR, "无效的数字");
		}

		// 文本框第一次设值时执行一次此方法,输入完成失去焦点时再执行一次
		public ValidationError isValid(Object value) {
			if ("0".equals(value))
				return ValidationError.error("不允许等于0");
			else
				return null;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -