首页 > 解决方案 > 数据绑定后组合框不可编辑

问题描述

Google-fu 没有透露任何信息,Stackoverflow 搜索显示了与我的情况不完全相同的类似问题。男孩,我在这里问了很多问题。

这不是考试题、家庭作业题或学校项目等。所以请不要费心回复这样的评论。这对我学习没有帮助

简单地说,我有一个项目存储在数据库中:

barcode (bigint), check_out_date (datetime), , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , due_date (datetime), is_available (String: True/False/Null, 我认为它比 UI 端的复选框更容易,也比在 DB 中使用 bit(1) 更容易,因为我无法正确绑定。) is_late (String: True/False/Null, same reason as above), name (varchar), notes (varchar),type (varchar)

is_lateis_available绑定到 2 个值(真/假)的组合框,数据库读取这些值并实际显示组合框中的值,但我无法编辑它们。作为参考,我在程序的不同部分有 2 个其他 Combobox,但它们没有绑定,因此可以工作。

我怎样才能使(强制?)这些组合框可编辑?感谢您的帮助,我很高兴成为这个社区的一员!

问题

相关代码:如果您看到格式/括号错误,那是因为我省略了所有实际有效的代码。该程序确实编译和运行没有问题。

private class editPanel extends FormLayout {
    private InventoryItem item;
    private TextField itemName, itemType, itemBarcode;
    private DateField checkOutDate, dueDate;
    private Button save, delete, cancel;
    private ComboBox<String> isAvailable, isLate;
    private TextArea notes;

    private Binder<InventoryItem> binder = new Binder<>(InventoryItem.class); 

    public editPanel() {
        initEditConf();
        initEditLayout();
        addListeners();
        setSizeUndefined();
        Responsive.makeResponsive(this);
        binder.bindInstanceFields(this);
    }

    private void addListeners() {

        isAvailable.addValueChangeListener(e -> {
            System.out.println("Test"); //still not editable with a listener
            //and still not editable by explicitly calling setEnabled(true), setReadOnly(false);
        });


    private void initEditLayout() {

        isAvailable = new ComboBox<String>("Availability");
        isAvailable.setItems("True", "False"); //should be managed by sys too
        isLate = new ComboBox<String>("Overdue");
        isLate.setEnabled(false);
        isLate.setDescription("Value is managed by the system");
        isLate.setIcon(VaadinIcons.QUESTION_CIRCLE_O);
        //isAvailable = new TextField("Availability");
        //isAvailable.setEnabled(false);
        //isLate = new TextField("Overdue");
        //isLate.setEnabled(false);

        cancel.addClickListener(e -> this.cancel());
        save.addClickListener(e -> this.save());
        delete.addClickListener(e -> this.delete());


        binder.forMemberField(checkOutDate).withConverter(new LocalDateToDateConverter());
        binder.forMemberField(dueDate).withConverter(new LocalDateToDateConverter());
        binder.forMemberField(itemName).asRequired().withValidator((string -> string != null && !string.isEmpty()), "Values cannot be empty").bind("name");
        binder.forMemberField(itemType).asRequired().withValidator((string -> string != null && !string.isEmpty()), "Values cannot be empty").bind("type");
        binder.forMemberField(itemBarcode).withConverter(new StringToLongConverter(itemBarcode.getValue())).bind("barcode");
        binder.forMemberField(isAvailable).bind("isAvailable");
        binder.forMemberField(isLate).bind("isLate");
    }

物品类别

@Table(name="item")
@Entity 
    public class InventoryItem implements Serializable, Cloneable {

    private static final long serialVersionUID = 5592334329765505365L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long barcode;
    @NotNull
    private String name, type;
    @Nullable
    private String notes;
    @Nullable
    private String isAvailable, isLate;
    @Nullable
    @Temporal(TemporalType.TIMESTAMP)
    private Date checkOutDate, dueDate;

    public InventoryItem() {}

    /* Excess constructors omitted */

    @Column(name="barcode")
    public Long getBarcode() {return barcode;}

    public void setBarcode(Long barcode) {this.barcode = barcode;}

    @Column(name="name")
    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    @Column(name="type")
    public String getType() {return type;}

    public void setType(String type) {this.type = type;}

    @Column(name="is_late")
    public String getisLate() {return isLate;}

    public void setLate(String isLate) {this.isLate = isLate;}

    @Column(name="availability")
    public String getisAvailable() {return isAvailable;}

    public void setAvailable(String isAvailable) {this.isAvailable = isAvailable;}

    @Column(name="notes") //bigtext?
    public String getNotes() {return notes;}

    public void setNotes(String notes) {this.notes = notes;}

    @Column(name="check_out_date", columnDefinition="DATETIME")
    public Date getCheckOutDate() {return checkOutDate;}

    public void setCheckOutDate(Date checkOutDate) {this.checkOutDate = checkOutDate;}

    @Column(name="due_date", columnDefinition="DATETIME")
    public Date getDueDate() {return dueDate;}

    public void setDueDate(Date dueDate) {this.dueDate = dueDate;}

}

标签: javaspring-data-jpavaadinvaadin8

解决方案


很好地解决您的问题!

setAvailable并且getIsAvailable不是很好的方法名称,因为它们不匹配。因此,正如您所知道的,使用setIsAvailable或将属性更改为available并使用setAvailable/getAvailable是正确的方法。

Vaadin Binder 使用 Java PropertyDescriptor 来查找 getter 和 setter。这只是在大写属性名称之前添加getorset前缀的一种情况。

如果您使用布尔值,则is前缀也可以用于 getter,从源代码中可以看出。

在这种情况下,您可以拥有一个布尔属性available,然后是访问setAvailableisAvailable方法。


推荐阅读