disable one item of a Radio button OptionGroup

Hi all,

I am using a Radio Button defined by a new OptionGroup. Here’s the code for the creation of the OptionGroup:

OptionGroup startTypes = new OptionGroup(null, Arrays.asList(new String {
“option 1”,
“option 2”,
“option 3”}));
startTypes.setNullSelectionAllowed(false);
startTypes.setWidth(“440px”);
startTypes.setPropertyDataSource(new MethodProperty(getForm(), “startType”));
dateLayout.addComponent(startTypes, 2, 0);
dateLayout.setComponentAlignment(startTypes, Alignment.TOP_LEFT);

As you can see the optionGroup contains a String with the items.

In some cases I want to set one of the items to readonly where selecting this one item should not be possible.
I can change the readonly for the complete optionGroup but don’t know how to set one item to readonly.

Can someone tell me if this is possible with Vaadin (using version 6.2.5) and if so how it can be done.

Thank you.

Richard Aalten

Hello

Unfortunately, OptionGroup doesn’t support this feature. But there is an enchantment
ticket
about it. A work around for the issue would be to use two OptionGroups, one for enabled items and another for disabled ones.

-Henri

OK,

Too bad.

We’ll just have to wait then for a new version of Vaadin.

Meanwhile we fixed it by showing an error message if the option is clicked when it should be disabled and automatically select the second option again.

Thank you for your response.

Richard

OptionGroup group = new OptionGroup(“My Disabled Group”);
group.addItem(“One”);
group.addItem(“Two”);
group.addItem(“Three”);

group.setItemEnabled(“Two”, false);

This post is too old, and no longuer true. The option’s value is ised as ItemId, necessary for OptionGroup’s disabledSet.

class SiteLanguagesOptionGroupFactory<D extends SelectFieldDefinition>  extends OptionGroupFieldFactory {
   @Override
   public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
    List<SelectFieldOptionDefinition> res = new ArrayList<>();
        Collection<Locale> locales = getLocales();
        for (Locale l : locales) {
            res.add(createOption(l));
        }
        
        return res;
    }

    /**
     * Configure a new {@link SelectFieldOptionDefinition} with give Locale language.
     * 
     * @param l The Locale to be represented by the generated option.
     * 
     * @return The option whose value is the Locale's language and label is Locale's
     * DisplayLanguage as returned by {@link Locale#getDisplayLanguage()}
     */
    private SelectFieldOptionDefinition createOption(Locale l) {
        SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
        option.setLabel(l.getDisplayLanguage());
        option.setValue(l.getLanguage());
        option.setName(l.getLanguage());
        if (i18nContentSupport.getFallbackLocale().equals(l)) {
            option.setSelected(true);
            ((OptionGroup) select).setItemEnabled(option.getValue(), false); // <-- HERE
        }
        return option;
    }
}