我有TableView
<TableView fx:id="tableView">
<columns>
<TableColumn prefWidth="220.0" text="Source">
<cellValueFactory>
<PropertyValueFactory property="sourceContract" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<GridRowModel sourceContract="some contract" />
</FXCollections>
</items>
</TableView>
和这些类
public class GridRowModel {
private ObjectProperty<ContractConfig> sourceContract = new SimpleObjectProperty<>();
public GridRowModel() {
}
public ObjectProperty<ContractConfig> sourceContractProperty() {
return sourceContract;
}
public ContractConfig getSourceContract() {
return sourceContract.get();
}
public void setSourceContract(ContractConfig sourceContract) {
this.sourceContract.set(sourceContract);
}
}
public class ContractConfig {
private String name;
private String userFriendlyName;
public ContractConfig() {
}
public ContractConfig(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void setUserFriendlyName(String userFriendlyName) {
this.userFriendlyName = userFriendlyName;
}
public String getName() {
return name;
}
public String getUserFriendlyName() {
return userFriendlyName;
}
}
我得到了这个明显的错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce some contract to class com.ui.util.ContractConfig.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:496)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
我也试过这个
public void setSourceContract(String sourceContract) {
ContractConfig cc = new ContractConfig();
cc.setUserFriendlyName(sourceContract);
this.sourceContract.set(cc);
}
但是我收到了这个错误
Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "sourceContract" does not exist or is read-only.
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:253)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
是否可以将ObjectProperty与FXML值一起使用?如果可以,我如何在FXML中使用我的ContractConfig对象?
答案 0 :(得分:2)
对您创建的类结构使用了错误的fxml代码。它看起来应该是这样的:
@NamedArg
您还可以将GridRowModel
的构造函数添加到<GridRowModel sourceContract="some contract" />
并使用
private final ObjectProperty<ContractConfig> sourceContract;
private GridRowModel(ContractConfig sourceContract) {
this.sourceContract = new SimpleObjectProperty<>(sourceContract);
}
public GridRowModel() {
this((ContractConfig) null);
}
public GridRowModel(@NamedArg("sourceContract") String sourceContract) {
this(new ContractConfig(sourceContract));
}
public void method(String[] args, String user){}