Java – Binding enum values to RadioButton in Java

data-bindingjavaradio-buttonswing

I am stuck in binding values from enum to RadioButton. Lets say I have a bean:

public class ValueObject {
  public enum ValueEnum {
    FIRST_VALUE,
    SECOND_VALUE
  }

  protected ValueEnum value;
}

I want to create Swing RadioButtons where user can select one from these two enum values. As I use Netbeans as IDE I would also like to do it using data binding, ie. bind radio buttons to {valueObject.value}. Any ideas? Thanks a lot!

Best Solution

Whow, the converter was a great idea! I created the converter below and initialized it with the enum value.

public class ObjectEqualityConverter extends Converter {

    private Object o;

    public ObjectEqualityConverter() {}

    public ObjectEqualityConverter(Object o) {
        this.o = o;
    }

    public Object convertForward(Object value) {
        return (o != null && o.equals(value)) ? Boolean.TRUE : Boolean.FALSE;
    }

    public Object convertReverse(Object value) {
        return (Boolean.TRUE.equals(value)) ? o : null;
    }
}