java - 在 Vaadin 7 中清除 FieldGroup

标签 java converters vaadin vaadin7

如何清除 FieldGroup 中的字段?在Vaadin 7 个应用程序?

当我调用 FieldGroup::setItemDataSource并传递 null,我得到文本“null”出现在 TextField 中由各种数据类型(数字、java.util.Date)支持的对象。我可以理解将“null”显示为 String 表示形式的空值。但是为什么默认情况下不出现那些“空”字符串呢?默认情况下,我在所有字段中看到空的 TextField 字符串。

例如,考虑下面的示例应用程序。在下面的截图中我们:

  1. 启动应用程序。用户未采取任何操作。
    所有字段均为空白,无字符串,无“空”文本。
  2. 用户点击“显示 Bernard”按钮。
  3. 用户点击“不显示任何人”按钮。此按钮调用 FieldGroup::setItemDataSource( null ),传递 null。
    数字和日期字段都显示“空”文本。为什么现在而不是在发布时?
    我怎样才能恢复到空白值?

应用启动

App launches. User takes no action.

用户点击“显示伯纳德”按钮

User clicks "Show Bernard" button.

用户点击“不显示”按钮

User clicks "Show No One" button.

这是 Java 8 中完整的单文件 Vaadin 7.5.3 应用程序(使用 Lambda 语法)。

package com.example.gridexample;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.data.util.converter.StringToDateConverter;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 *
 */
@Theme ( "mytheme" )
@Widgetset ( "com.example.gridexample.MyAppWidgetset" )
public class MyUI extends UI
{

    PropertysetItem itemAaron = null;
    PropertysetItem itemBernard = null;
    TextField nameField = null;
    TextField ageField = null;
    TextField dateOfBirthField = null;
    FieldGroup fieldGroup = null;
    ZoneId zone = ZoneId.systemDefault(); // Or ZoneId.of( "America/Montreal" ) ;

    @Override
    protected void init ( VaadinRequest vaadinRequest ) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin( true );
        layout.setSpacing( true );
        setContent( layout );

        Button buttonAaron = new Button( "Show Aaron" );
        buttonAaron.addClickListener( ( ClickEvent event ) -> {
            showAaron();
        } );
        layout.addComponent( buttonAaron );

        Button buttonBernard = new Button( "Show Bernard" );
        buttonBernard.addClickListener( ( ClickEvent event ) -> {
            showBernard();
        } );
        layout.addComponent( buttonBernard );

        Button buttonNoOne = new Button( "Show No One" );
        buttonNoOne.addClickListener( ( ClickEvent event ) -> {
            showNoOne();
        } );
        layout.addComponent( buttonNoOne );

        this.itemAaron = new PropertysetItem();
        this.itemAaron.addItemProperty( "name" , new ObjectProperty<>( "Aaron" ) );
        this.itemAaron.addItemProperty( "age" , new ObjectProperty<>( 42 ) );
        this.itemAaron.addItemProperty( "dateOfBirth" , new ObjectProperty<>( Date.from( ZonedDateTime.now( this.zone ).minusYears( 42 ).toInstant() ) ) );

        this.itemBernard = new PropertysetItem();
        this.itemBernard.addItemProperty( "name" , new ObjectProperty<>( "Bernard" ) );
        this.itemBernard.addItemProperty( "age" , new ObjectProperty<>( 24 ) );
        this.itemBernard.addItemProperty( "dateOfBirth" , new ObjectProperty<>( Date.from( ZonedDateTime.now( this.zone ).minusYears( 24 ).toInstant() ) ) );

        this.nameField = new TextField( "Name:" );
        this.ageField = new TextField( "Age:" );
        this.dateOfBirthField = new TextField( "Date Of Birth:" );

        // Customize the Converter assigned to our date-time field. A StringToDateConverter object is assigned by default.
        //this.dateOfBirthField.setConverter( new StringToDateConverter() );  // Actually the default.
        //  this.dateOfBirthField.setConverter( new SpecialStringToDateConverter() );
        layout.addComponent( this.nameField );
        layout.addComponent( this.ageField );
        layout.addComponent( this.dateOfBirthField );

        this.fieldGroup = new FieldGroup();
        this.fieldGroup.bind( this.nameField , "name" );
        this.fieldGroup.bind( this.ageField , "age" );
        this.fieldGroup.bind( this.dateOfBirthField , "dateOfBirth" );
        this.fieldGroup.setReadOnly( true );

    }

    private void showAaron () {
        this.fieldGroup.setItemDataSource( this.itemAaron );
    }

    private void showBernard () {
        this.fieldGroup.setItemDataSource( this.itemBernard );
    }

    private void showNoOne () {
        this.fieldGroup.setItemDataSource( null );
    }

    @WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
    @VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
    public static class MyUIServlet extends VaadinServlet
    {
    }

}

最佳答案

在点击任何按钮之前(初始状态)这些行被执行:

TextField nameField = null;
//...
this.nameField = new TextField( "Name:" );

文档说:

public TextField(java.lang.String caption)

Constructs an empty TextField with given caption.

因此您应用程序中的所有文本字段都是空值。但是......之后你使用:

this.fieldGroup.bind( this.nameField , "name" );

该值应该为空吗? [bind()]( https://vaadin.com/api/com/vaadin/data/fieldgroup/FieldGroup.html#bind(com.vaadin.ui.Field , java.lang.Object)) 的文档再次说:

Binds the field with the given propertyId from the current item. If an item has not been set then the binding is postponed until the item is set using setItemDataSource(Item).

所以必须要用这个方法才能看到bind的效果。

您问:但为什么默认情况下不出现那些“空”字符串?

我希望我的解释对您来说是清楚的,每个 TextField 最初都有 empty 值。而调用bind()函数后,只有在setItemDataSource(Item)之后才会影响 View ,但是你在button的clickListener中调用这个函数要晚很多。所以一开始没问题,所有字段都有空值。

关于java - 在 Vaadin 7 中清除 FieldGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909474/

相关文章:

mysql - 如何将UTC timediff字符串转换为mysql datetime

java - Vaadin上传功能

java - OpenStreetMap 标签 - 配置

Vaadin 14 纸张 slider 销属性

java - 如何在此代码中替换 com.sun.image.codec.jpeg.JPEGImageEncoder?

java - Java进程段错误

java - Android 9.0使用intent打开图像总是显示黑色结果

java - Spring未初始化bean(dbunit);我错过了什么?

wpf - 如何在WPF ListView中以不同的颜色显示字符串?

parsing - SID文件格式解析