Google-fu 没有透露任何信息,Stackoverflow 搜索显示了类似的问题,但机器人与我的情况并不完全一致。天哪,我在这里问了很多问题。
这不是考试问题、家庭作业问题或学校项目等。所以请不要费心回复这样的评论。这对我的学习没有帮助
简单地说,我在数据库中存储了一个项目:
条形码(bigint)
、check_out_date(日期时间)
、due_date(日期时间)
、is_available(字符串:True/False/Null
,我认为它比 UI 端的复选框更容易,也比在数据库中使用 bit(1) 更容易,因为我无法正确绑定(bind)。) is_late (String: True/False/Null,原因与上述相同)
、名称 (varchar)
、注释 (varchar)
、类型 (varchar)
is_late
和 is_available
绑定(bind)到一个包含 2 个值(True/False)的组合框,数据库读取这些值并实际在组合框中显示这些值,但我我无法编辑它们。作为引用,我在程序的不同部分还有另外 2 个组合框,但它们没有绑定(bind),因此可以工作。
如何使(强制?)这些组合框可编辑?感谢您的帮助,我很高兴成为这个社区的一员!
相关代码:如果您看到格式/括号错误,那是因为我省略了所有实际有效的代码。该程序确实编译并运行没有问题。
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;}
}
最佳答案
很好地解决了您的问题!
setAvailable
和 getIsAvailable
不是很好的方法名称,因为它们不匹配。正如您所想,使用 setIsAvailable
或将属性更改为 available
并使用 setAvailable
/getAvailable
是正确的方法。
Vaadin Binder 使用 Java PropertyDescriptor 来查找 getter 和 setter。只需在大写属性名称之前添加 get
或 set
前缀即可。
如果您使用 boolean 值,则 is
前缀也可用于 getter,如 from the source code 所示。 .
在这种情况下,您可以有一个 boolean 属性 available
,然后是 setAvailable
和 isAvailable
访问器方法。
关于java - 数据绑定(bind)后组合框不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931989/