How do I bind radiogroup to a java 5 enum?

asked 2009-06-02 12:56:58 +0800

fmcypriano gravatar image fmcypriano
612 1 7


I need to bind a radiogroup to a property that is of type enum (java 5). I think it is possible with TypeConverters but I couldn't find a documentation to understand how it works, anyone has any tips or examples?

SomeEnumType {A, B, C}
class WithEnum {
    SomeEnumType propertyName
    // getters and setters
<radiogroup // how to bind it? ...

Felipe Cypriano

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-06-02 15:39:14 +0800

robertpic71 gravatar image robertpic71
1275 1

There is no model for the radiogroup - so you could not work with @databinding to setup up the group.

You could try to mix EL's with @databinding.

<radiogroup selectedItem="@{selectedItem}">
<radio foreach="${tryEnum}"...>


Check my homepage for some simple examples.

link publish delete flag offensive edit

answered 2009-06-02 15:44:46 +0800

fmcypriano gravatar image fmcypriano
612 1 7

updated 2009-06-02 15:54:47 +0800

I think it is working:

package vc.zk.databinding;

import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.RadiogroupSelectedItemConverter;

 * Created by Felipe Cypriano
 * Date: 02/06/2009
 * Time: 09:24:34
 * <p>The radio's value attribute must be the same as the enum constant</p>
 * <strong>Thread Safe</strong>
public class EnumRadiogroupConverter<T extends Enum> extends RadiogroupSelectedItemConverter {
    private final Class<T> className;

    public EnumRadiogroupConverter(Class<T> className) {
        this.className = className;

    public Object coerceToUi(Object val, Component component) {
        if (val instanceof Enum) {
            Enum value = (Enum) val;
            String constanteName = value.name();
            return super.coerceToUi(constanteName, component);
        } else if (val == null){
            return null;
        } else {
            throw new IllegalArgumentException("val Object must be an Enum");

    public Object coerceToBean(Object val, Component component) {
        String enumName = (String) super.coerceToBean(val, component);
        return Enum.valueOf(className, enumName);

// public class OrigemEstoqueRadiogroupConverter extends EnumRadiogroupConverter<OrigemEstoque>{ ...
<radiogroup selectedItem="@{pedidoVenda.origemEstoque, converter='vc.zk.databinding.OrigemEstoqueRadiogroupConverter'}">

I'm doing the tests right now.

link publish delete flag offensive edit

answered 2009-06-02 16:26:47 +0800

robertpic71 gravatar image robertpic71
1275 1

>> I think it is working
Yes, but like my example only for the selectedItem. You could not load/create the radiogroup with a model like i.e. a combobox.


link publish delete flag offensive edit

answered 2009-10-20 10:33:12 +0800

xmedeko gravatar image xmedeko
1021 1 16

@fmcypriano : thanks, exactly what i was looking for!

link publish delete flag offensive edit

answered 2009-10-27 11:37:22 +0800

dastultz gravatar image dastultz
797 8

Good work, Felipe, this is working for me.


link publish delete flag offensive edit

answered 2009-10-27 12:01:24 +0800

kesavkolla gravatar image kesavkolla
180 3

Hi I am running my app in Google App engine and this type converter is not working for me. I am getting the following exception:

Failed to load /WEB-INF/views/npi.zul

Cause: java.lang.InstantiationException: com.bodyfs.ui.GenderRadioConverter.<init>()
org.zkoss.zk.ui.UiException: java.lang.InstantiationException: com.bodyfs.ui.GenderRadioConverter.<init>()
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at org.zkoss.lang.Classes.newInstance(Classes.java:76)
at org.zkoss.lang.Exceptions.wrap(Exceptions.java:162)
at org.zkoss.zk.ui.UiException$Aide.wrap(UiException.java:44)
at org.zkoss.zkplus.databind.Binding.setConverter(Binding.java:312)

Any ideas how to make this to work?

link publish delete flag offensive edit

answered 2011-06-10 15:22:18 +0800

sergio28julio gravatar image sergio28julio

updated 2011-06-10 15:23:37 +0800

I also got that InstantiationException when I was trying fmcypriano's EnumRadiogroupConverter.

I think this exception is due to a failure related to default constructor of OrigemEstoqueRadiogroupConverter. So I modified original fmcypriano's solution ensuring default constructor exists and works, then data binding worked too!!!.

BTW, I had to get rid of generics in: EnumRadiogroupConverter<T extends Enum> ...

Thanks fmcypriano for that solution it help me a lot. ;) :)


link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2009-06-02 12:56:58 +0800

Seen: 2,903 times

Last updated: Jun 10 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More