0

我陷入了其中一个问题。假设我有一个电话号码字段010-9999-9999,这个号码应该分成 3 个文本字段。我试图这样做,但我在所有三个文本字段中只得到前缀编号,即 010。我正在使用 jface 数据绑定。

我创建模型为

class A{
    String phoneNo;
}

Jface 数据绑定:

IObservableValue ssn1TextTextObserveWidget = SWTObservables.observeText(text_ph11, SWT.Modify);
IObservableValue ssn2TextTextObserveWidget = SWTObservables.observeText(text_ph2, SWT.Modify);
IObservableValue ssn2TextTextObserveWidget1 = SWTObservables.observeText(text_ph3, SWT.Modify);

IObservableValue simpleTableViewerSSN1ObserveDetailValue = BeansObservables.observeDetailValue(simpleTableViewerSelectionObserveSelection_employee, "phoneNo", String.class);
IObservableValue simpleTableViewerSSN2ObserveDetailValue = BeansObservables.observeDetailValue(simpleTableViewerSelectionObserveSelection_employee, "phoneNo", String.class);

IObservableValue simpleTableViewerSSN2ObserveDetailValue = BeansObservables.observeDetailValue(simpleTableViewerSelectionObserveSelection_employee, "phoneNo", String.class);

数据绑定

bindingContext.bindValue(simpleTableViewerSSN1ObserveDetailValue, ssn1TextTextObserveWidget, null, null);
bindingContext.bindValue(simpleTableViewerSSN2ObserveDetailValue, ssn2TextTextObserveWidget, null, null);
bindingContext.bindValue(simpleTableViewerSSN2ObserveDetailValue, ssn2TextTextObserveWidget1, null, null);

当我尝试执行此值时,只会在所有三个字段中填充前缀,即 010-010-010。请帮我解决这个问题。

4

2 回答 2

0

如果您希望将您的电话号码分成三个不同的小部件,您可能需要将电话号码保存在三个不同的属性中,每个属性都分配了电话号码的不同部分。

可能有某种方法可以将字符串值的“部分”绑定到小部件,可能是通过 getter 方法,但请等待,让在这方面更有知识的人为您提供答案。

于 2013-01-17T10:59:41.073 回答
0

这是可以修改以适用于您的案例的示例代码。IConverter用于UpdateValueStrategy修改将设置/获取模型到/从目标文本字段的值。

//GUI class

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, false));

    Label lbl = new Label(shell, SWT.NONE);
    lbl.setText("Name");
    final Text firstName = new Text(shell, SWT.BORDER);
    final Text lastName = new Text(shell, SWT.BORDER);
    final Name name = new Name();
    SWTObservables.getRealm(display).exec(new Runnable() {

      @Override
      public void run() {

        // TODO Auto-generated method stub
        Realm realm = SWTObservables.getRealm(display);
        DataBindingContext cxt = new DataBindingContext(realm);

        ISWTObservableValue firstNameObservable = SWTObservables.observeText(firstName, new int[] { SWT.Modify });
        ISWTObservableValue lastNameObservable = SWTObservables.observeText(lastName, new int[] { SWT.Modify });

        final IObservableValue nameObservable = BeansObservables.observeValue(realm, name, "name");

        UpdateValueStrategy firstNameTxtToName = new UpdateValueStrategy();

        firstNameTxtToName.setConverter(new IConverter() {

          @Override
          public Object getToType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object getFromType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object convert(Object fromObject) {

            String val = fromObject.toString();
            Object beanVal = nameObservable.getValue();
            if (beanVal != null) {
              String beanString = beanVal.toString();
              int i = beanString.indexOf(',');
              if (i != -1) {
                return val + beanString.substring(i);
              }
            }
            return val;
          }
        });

        UpdateValueStrategy nameToFirstName = new UpdateValueStrategy();

        nameToFirstName.setConverter(new IConverter() {

          @Override
          public Object getToType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object getFromType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object convert(Object fromObject) {

            if (fromObject != null) {
              Object objVal = nameObservable.getValue();
              if (objVal != null) {
                String val = objVal.toString();
                int i = val.indexOf(',');
                if (i != -1) {
                  return val.substring(0, i);
                } else {
                  return val;
                }
              }
            }
            return "";
          }
        });

        UpdateValueStrategy lastNameTxtToName = new UpdateValueStrategy();

        lastNameTxtToName.setConverter(new IConverter() {

          @Override
          public Object getToType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object getFromType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object convert(Object fromObject) {

            String val = fromObject.toString();
            Object beanVal = nameObservable.getValue();
            if (beanVal != null) {
              String beanString = beanVal.toString();
              int i = beanString.indexOf(',');
              String fName = beanString;
              if (i != -1) {
                fName = beanString.substring(0, i + 1);
              } else {
                fName = fName + ",";
              }
              val = fName + val;
            }
            return val;
          }
        });

        UpdateValueStrategy nameToLastName = new UpdateValueStrategy();

        nameToLastName.setConverter(new IConverter() {

          @Override
          public Object getToType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object getFromType() {

            // TODO Auto-generated method stub
            return String.class;
          }

          @Override
          public Object convert(Object fromObject) {

            if (fromObject != null) {
              String val = fromObject.toString();
              int i = val.indexOf(',');
              if (i != -1) {
                return val.substring(i + 1);
              }
            }
            return "";
          }
        });
        cxt.bindValue(firstNameObservable, nameObservable, firstNameTxtToName, nameToFirstName);

        cxt.bindValue(lastNameObservable, nameObservable, lastNameTxtToName, nameToLastName);
      }
    });

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




//POJO


public class Name {

  private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

  public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {

    changeSupport.addPropertyChangeListener(propertyName, listener);
  }

  public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {

    changeSupport.removePropertyChangeListener(propertyName, listener);
  }

  public void addPropertyChangeListener(PropertyChangeListener listener) {

    changeSupport.addPropertyChangeListener(listener);
  }

  public void removePropertyChangeListener(PropertyChangeListener listener) {

    changeSupport.removePropertyChangeListener(listener);
  }

  public void firePropertyChangeEvent(PropertyChangeEvent evt) {

    changeSupport.firePropertyChange(evt);
  }

    String name;


    /**
     * @return the name
     */
    public String getName() {

      return name;
    }


    /**
     * @param name the name to set
     */
    public void setName(String name) {

      this.name = name;

      System.out.println(name);
    }

  }
于 2013-01-28T18:52:40.167 回答