如何将枚举值与SWT Combo绑定?(无需ComboBoxViewer)
Got it, let's tackle this problem. The issue here is that PojoProperties.list() is designed to bind to instance properties of an object, but Gender.values() is a static method—there's no instance property on the enum class that exposes the list of enum constants. So we can't use PojoProperties here; instead, we need to create an observable list directly from the enum values and bind that to the Combo's items.
Step 1: Polish the Gender Enum
First, let's add a getter to your Gender enum so we can access the custom display string you defined in the constructor:
public enum Gender { MALE("male"), FEMALE("female"); private final String displayName; Gender(String displayName) { this.displayName = displayName; } // Getter to retrieve the human-readable display string public String getDisplayName() { return displayName; } }
Step 2: Create an Observable List from Enum Values
Use JFace Databinding's ObservableLists utility to turn the static enum array into an observable list:
import org.eclipse.core.databinding.observable.list.IObservableList; import org.eclipse.core.databinding.observable.list.ObservableLists; import java.util.Arrays; // Create an observable list containing all Gender enum constants IObservableList<Gender> genderList = ObservableLists.observableList(Arrays.asList(Gender.values()));
Step 3: Bind the Observable List to the Combo
Now, use a DataBindingContext to link this list to your Combo's items property. We'll add a converter to map each enum instance to its display string (so the Combo shows "male"/"female" instead of raw enum names):
import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.databinding.conversion.IConverter; import org.eclipse.jface.databinding.swt.WidgetProperties; DataBindingContext dbc = new DataBindingContext(); // Bind the enum list to the Combo's items dbc.bindList( WidgetProperties.items().observe(combo), // Target: Combo's items property genderList, // Model: Our observable enum list null, // No model-to-target update strategy (enum values are static) // Converter to turn Gender enum into display text new IConverter<Object, String>() { @Override public String convert(Object fromObject) { if (fromObject instanceof Gender) { return ((Gender) fromObject).getDisplayName(); } return fromObject != null ? fromObject.toString() : ""; } } );
Step 4: Keep Your Existing Selection Binding
Your original code to bind the Combo's selection to the Person's gender property works perfectly here—since the Combo's selected item will be the actual Gender enum instance, it matches the model's type:
IObservableValue<?> observeWidget = WidgetProperties.selection().observe(combo); IObservableValue<?> observeModel = PojoProperties.value(Person.class, "gender").observe(person); dbc.bindValue(observeWidget, observeModel);
Quick Notes
- Since enum constants are fixed, the
genderListwon't change during runtime—no need to handle dynamic updates. If you ever needed a mutable enum list (uncommon), useWritableListinstead. - If you don't need custom display text and are okay with showing "MALE"/"FEMALE" in the Combo, you can skip the converter entirely—just bind the list directly to the Combo's items.
内容的提问来源于stack exchange,提问作者Shramik

