SWT组合中的空条目

时间:2015-12-09 15:29:13

标签: java combobox swt

我有一个SWT组合,它有一个空条目和一些值。当我选择空条目时,它不会触发任何事件。但我需要发起更新事件。如果我调用firePropertyChange()方法,我总是在控制台中得到以下异常。

在importMap中,它具有以下值 - {,1,2,3,4}。第一个值是空字符串。

我正在绑定Databindingscontext

中的组合值
IObservableValue observeSelectionCImportObserveWidget = WidgetProperties.selection().observe(cImport);
IObservableValue importModelObserveValue = BeanProperties.value("import").observe(model);
bindingContext.bindValue(observeSelectionCImportObserveWidget, importModelObserveValue, null, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));

IObservableValue observeSingleSelectionIndexCImportObserveWidget = WidgetProperties.singleSelectionIndex().observe(cImport);
IObservableValue importIndexModelObserveValue = BeanProperties.value("importIndex").observe(model);
bindingContext.bindValue(observeSingleSelectionIndexCImportObserveWidget, importIndexModelObserveValue, null, null);

IObservableList itemsCImportObserveWidget = WidgetProperties.items().observe(cImport);
IObservableList importComboModelObserveList = BeanProperties.list("importCombo").observe(model);
bindingContext.bindList(itemsCImportObserveWidget, importComboModelObserveList, null, null);

public void setImport(String value){
        if(value.equals(""))
            return;
        long index = 0;

        for (Entry<Long, String> entry : importmap.entrySet()) {
            if (entry.getValue().equals(value)) {
                index = entry.getKey();
                break;
            }
        }
        if(ag.getImport() == index) return; // ag is a class which has getters and setters for Import.
        ag.setImport(index);
        setPriority(); // this method gets the value ag.getImport() and does some operation and updates the values in a text field

        IsDirty();
    }

    public String getImport(){
        if(ag.getImport() < 1)
            return "";
        return importMap.get(ag.getImport());

    }
    public int getImportIndex(){
        if(ag.getImport() < 1)
            return -1;
        long index = 0;
        for (Entry<Long, String> entry : importmap.entrySet()) {
            if (entry.getKey() == ag.getImport()) {
                return (int) index;
            }
            index++;
        }
        return -1;
    }
    public List<String> getImportCombo(){
        List<String> importValues = new ArrayList<String>();
        if(importMap != null && importmap.size() > 0)
            importtValues.addAll(importmap.values());
        return importValues;
    }

这是我在组合框中选择项目时遇到的以下异常

WARN : 16:12:49,983  - TcLogger$IC_PrintStream.log:?
java.lang.NullPointerException
    at org.eclipse.core.internal.databinding.beans.BeanPropertyHelper.writeProperty(BeanPropertyHelper.java:51)
    at org.eclipse.core.internal.databinding.beans.BeanValueProperty.doSetValue(BeanValueProperty.java:51)
    at org.eclipse.core.databinding.property.value.ValueProperty.setValue(ValueProperty.java:67)
    at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.doSetValue(SimplePropertyObservableValue.java:104)
    at org.eclipse.core.databinding.observable.value.AbstractObservableValue.setValue(AbstractObservableValue.java:55)
    at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.setValue(DecoratingObservableValue.java:103)
    at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.setValue(DecoratingObservableValue.java:103)
    at org.eclipse.core.databinding.UpdateValueStrategy.doSet(UpdateValueStrategy.java:486)
    at org.eclipse.core.databinding.ValueBinding$4.run(ValueBinding.java:197)
    at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
    at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
    at org.eclipse.core.databinding.ValueBinding$3.run(ValueBinding.java:188)
    at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
    at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
    at org.eclipse.core.databinding.ValueBinding.doUpdate(ValueBinding.java:151)
    at org.eclipse.core.databinding.ValueBinding.access$4(ValueBinding.java:140)
    at org.eclipse.core.databinding.ValueBinding$1.handleValueChange(ValueBinding.java:45)
    at org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:62)
    at org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:119)
    at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.fireValueChange(DecoratingObservableValue.java:55)
    at org.eclipse.core.databinding.observable.value.DecoratingObservableValue.handleValueChange(DecoratingObservableValue.java:93)
    at org.eclipse.core.databinding.observable.value.DecoratingObservableValue$1.handleValueChange(DecoratingObservableValue.java:67)
    at org.eclipse.core.databinding.observable.value.ValueChangeEvent.dispatch(ValueChangeEvent.java:62)
    at org.eclipse.core.databinding.observable.ChangeManager.fireEvent(ChangeManager.java:119)
    at org.eclipse.core.databinding.observable.value.AbstractObservableValue.fireValueChange(AbstractObservableValue.java:71)
    at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.notifyIfChanged(SimplePropertyObservableValue.java:120)
    at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.access$1(SimplePropertyObservableValue.java:112)
    at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue$2.run(SimplePropertyObservableValue.java:66)
    at org.eclipse.core.databinding.observable.Realm$1.run(Realm.java:148)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.databinding.observable.Realm.safeRun(Realm.java:152)
    at org.eclipse.core.databinding.observable.Realm.exec(Realm.java:170)
    at org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue$1.handleEvent(SimplePropertyObservableValue.java:63)
    at org.eclipse.core.databinding.property.NativePropertyListener.fireChange(NativePropertyListener.java:63)
    at org.eclipse.jface.internal.databinding.swt.WidgetListener.handleEvent(WidgetListener.java:55)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2629)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2593)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2427)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:670)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:663)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at com.teamcenter.rac.aifrcp.Application.runApplication(Unknown Source)
    at com.teamcenter.rac.aifrcp.Application.start(Unknown Source)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

1 个答案:

答案 0 :(得分:1)

如果您正在使用&#39; importIndex&#39;你需要一个&#39; setImportIndex&#39;方法,以便更改的值可以保存在bean中。