本文介绍了VAADIN:为什么我不能设置转换器到ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个转换器:

I've made a converter:

public class BooleanToDateConverter implements Converter<Boolean, Date> {
    private static final long serialVersionUID = 1L;

    @Override
    public Date convertToModel(Boolean value, Class<? extends Date> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if (value == true) {
            return new Date();
        } else {
            return null;
        }
    }

    @Override
    public Boolean convertToPresentation(Date value, Class<? extends Boolean> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if (value == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Class<Date> getModelType() {
        return Date.class;
    }

    @Override
    public Class<Boolean> getPresentationType() {
        return Boolean.class;
    }
}

然后我有一个Vaadin ComboBox myComboBox
我尝试设置我的转换器它:

Then I have a Vaadin ComboBox myComboBoxI try to set my converter to it:

myComboBox.setConverter(new BooleanToDateConverter());

然后我在Eclipse中得到一个错误:

Then I get an error in Eclipse saying:

The method setConverter(Class<?>) in the type AbstractField<Object> is not applicable for the arguments (BooleanToDateConverter)

但是,我看到其他转换器使用类似,他们不会得到错误。为什么?

However, I've seen other converters being used similarly and they don't get errors. Why?

推荐答案

您的代码无法编译,因为没有 setConverter()方法可用在 ComboBox 类,适合您的自定义转换器。让我解释一下如何在选择组件上使用转换器,以及在 ComboBox 上设置转换器的特定方法签名背后的想法。

Your code cannot be compiled because there is no setConverter() method available on class ComboBox that fits your custom converter. Let me explain how converters are used on select components and what is the idea behind the specific method signatures you find for setting converters on a ComboBox.

ComboBox 提供 setConverter()的两个重载版本:

ComboBox provides two overloaded versions of setConverter():


  • setConverter(Class<?> datamodelType):为给定数据模型类型设置预注册转换器

  • setConverter(Converter< Object,?> converter):设置具体的转换器实例

  • setConverter(Class<?> datamodelType): set a pre-registered converter for the given data model type
  • setConverter(Converter<Object, ?> converter): set a concrete converter instance

这两个方法实际上都继承自 AbstractField< T> 类,其中 T 是由字段管理的数据类型(例如,对于 DateField Date Object for ComboBoxes )。转换器通常用于在呈现类型(例如UI上的值的文本表示)和其内部模型类型(例如日期,货币值或自定义JavaBean)之间进行转换。因此,例如,如果您有一个 Label ,您可以使用 StringToDateConverter 正确显示 Date 对象,它已经以正确的本地化方式设置为标签的值。

Both of these methods are actually inherited from class AbstractField<T> where T is the data type managed by the field (e.g. Strings for text fields, Date for a DateField, Object for ComboBoxes). A converter is typically used to convert between a presentation type (such as the textual representation of a value on the UI) and its internal model type (such as a date, a monetary value or a custom JavaBean). So, for instance, if you have a Label you can use a StringToDateConverter to correctly display a Date object, which has been set as the value of the Label, in a properly localized way.

如何使用选择组件如 ComboBox ?这里类型 T Object 。选择组件的数据类型实际上表示从底层容器数据源选择的项目的项目ID。因此,如果您使用 BeanItemContainer 作为 ComboBox 的数据源,容器的项目ID ComboBox 的值是包含的JavaBean对象本身。项目ID的具体类型取决于所使用的容器实现。因此,选择组件是 Field 值类型为 Object 的组件。换句话说,选择组件使用 Object 作为显示类型。

How is that with select components such as ComboBox? Here the type T is Object. The data type of a select component actually represents the item ID of the selected item from the underlying container data source. So, if you use a BeanItemContainer as the data source of a ComboBox, the container's item IDs (and hence the selected value of the ComboBox) are the contained JavaBean objects themselves. The concrete type of the item IDs depends on the container implementation used. Therefore, select components are Field components with value type Object. In other words, select components use Object as presentation type.

这就是为什么你只能设置一个转换器实例其中 PRESENTATION 类型为 Object 的select组件。模型类型可以自由选择。这也解释了为什么你不能设置一个转换器与布尔和模型类型日期 ComboBox - ComboBox 不使用 Boolean 作为显示类型。

That is why you can only set a converter instance on a select component whose generic PRESENTATION type is Object. The model type can be chosen freely. And this also explain why you can't set a converter with presentation type Boolean and model type Date on a ComboBox -- ComboBox doesn't use Boolean as presentation type.

我写了一篇关于Vaadin FieldGroups 的博客文章,它也提供了一个很好的例子, 转换器< Object,?> ComboBox 。您可以在。

I wrote a blog post about Vaadin FieldGroups which also provides a good example for a use case when to use a Converter<Object, ?> on a ComboBox. You can find this article at http://blog.oio.de/2014/04/25/select-nested-javabeans-vaadin-fieldgroup/.

我不知道你想用代码实现什么,因为在 Boolean 的表示类型和 Date 的模型类型之间的转换器没有多大意义。我只能猜测你想实现某种决策逻辑,也许是决定一个日期是否已经设置?在这种情况下,您需要采取不同的方法。

I don't know what you want to achieve with your code, because a converter between a presentation type of Boolean and a model type of Date doesn't make much sense. I can only guess that you want to implementat some sort of decision logic, maybe to decide whether or not a date has been set? In that case you need to take a different approach.

如需参考,请查看。

这篇关于VAADIN:为什么我不能设置转换器到ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:35