java - SWT Widgets 绑定(bind)问题

标签 java eclipse swt eclipse-rcp

我正在开发 Eclipse RCP 应用程序,我需要创建一个更大的对话框,我需要在其中处理数据绑定(bind)。使用 TextFields 可以很好地进行绑定(bind)。我对 2 个 SWT 小部件有问题 - Combo、Spinner。 Spinner 中会有数字,我也可以在其中书写。 Spinner 仅使用字符串数组中的值。

对于绑定(bind),我使用以下代码:

private void doBinding(MyPartComposite comp) {
    /* code for spinner */
    IObservableValue model = PojoProperties.value(MyPart.class, "age").observe(comp.result); //$NON-NLS-1$
    ISWTObservableValue target = WidgetProperties.text(SWT.Modify).observe(comp.ageSpinner);
    UpdateValueStrategy targetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
    this.dataBindingContext.bindValue(target, model, targetToModel, null);

    /* code for combo */
    model = PojoProperties.value(MyPart.class, "gender").observe(comp.result); //$NON-NLS-1$
    target = WidgetProperties.text(SWT.Modify).observe(comp.genderCombo);
    targetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
    this.dataBindingContext.bindValue(target, model, targetToModel, null);

}

最佳答案

Spinner 没有文本属性,而是将其绑定(bind)到其选择。下面的示例对我有用,我假设年龄属性是整数,性别是字符串。

IObservableValue observeSelectionAgeSpinnerObserveWidget = WidgetProperties.selection().observe(ageSpinner);
IObservableValue ageResultObserveValue = PojoProperties.value("age").observe(comp.result);
dataBindingContext.bindValue(observeSelectionAgeSpinnerObserveWidget, ageResultObserveValue, null, null);
//
IObservableValue observeSingleSelectionGenderCombo = ViewerProperties.singleSelection().observe(genderCombo);
IObservableValue genderResultObserveValue = PojoProperties.value("gender").observe(comp.result);
dataBindingContext.bindValue(observeSingleSelectionGenderCombo, genderResultObserveValue, null, null);

关于java - SWT Widgets 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982025/

相关文章:

java - 将 Eclipse 生成的 GUI 表单导入 IntelliJ

java - Control类的moveBelow方法有什么作用?

java - 当我发现自己是 "over-casting"时该怎么办?

java.lang.ClassNotFoundException : javax. faces.application.ApplicationFactory

java - Android Quiz App 的答案即使已正确保存也无法正常工作

java - Eclipse Juno 提示未安装 SWT,但可用软件面板提示已安装

java - SWT TableViewer 单元格不显示图像,除非单击可编辑单元格

java - 为什么这个 java 程序的结果是 '44' ?

java - 暂停 Android 应用程序,直到提供输入

java - 不能既在 Eclipse 中将 JAR 添加到我的 Java 项目又在 Git 中看到它