Drop down menu 如何将复杂类型设置为ChoiceBox项?

Drop down menu 如何将复杂类型设置为ChoiceBox项?,drop-down-menu,javafx,tableview,Drop Down Menu,Javafx,Tableview,我有这个TableView <TableView fx:id="tableView"> <columns> <TableColumn prefWidth="220.0" text="Reference" editable="true"> <cellFactory> <fx:define> <FXCollections fx:factory="observabl

我有这个
TableView

<TableView fx:id="tableView">
  <columns>
    <TableColumn prefWidth="220.0" text="Reference"
      editable="true">
      <cellFactory>
        <fx:define>
          <FXCollections fx:factory="observableArrayList" fx:id="items">
            <Contract userFriendlyName=""/>
            <Contract userFriendlyName="M1"/>
            <Contract userFriendlyName="M2"/>
            <Contract userFriendlyName="M3"/>
            <Contract userFriendlyName="M4"/>
            <Contract userFriendlyName="Q1"/>
            <Contract userFriendlyName="Q2"/>
            <Contract userFriendlyName="SWS1"/>
            <Contract userFriendlyName="SWS2"/>
          </FXCollections>
        </fx:define>
        <ChoiceBoxTableCellFactory
          maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" items="$items">
        </ChoiceBoxTableCellFactory>
      </cellFactory>
      <cellValueFactory>
        <PropertyValueFactory property="referenceContract" />
      </cellValueFactory>
    </TableColumn>
  </columns>
  <items>
    <FXCollections fx:factory="observableArrayList">
      <GridRowModel>
        <referenceContract>
          <Contract name="" userFriendlyName="" />
        </referenceContract>
      </GridRowModel>
    </FXCollections>
  </items>
</TableView>
我在
表格列上有一个带有
选择框的
cellFactory

public class ChoiceBoxTableCellFactory<S, T> implements Callback<TableColumn<S, Contract>, TableCell<S, Contract>> {

  private ObservableList<Contract> items;
  private double maxHeight;
  private double maxWidth;

  public ChoiceBoxTableCellFactory() {
  }

  public ChoiceBoxTableCellFactory(
    @NamedArg("maxHeight") double maxHeight,
    @NamedArg("maxWidth") double maxWidth,
    @NamedArg("items") ObservableList<Contract> items) {
    this.maxHeight = maxHeight;
    this.maxWidth = maxWidth;
    this.items = items;
  }

  @Override
  public TableCell<S, Contract> call(TableColumn<S, Contract> param) {
    return new TableCell<S, Contract>() {
      ChoiceBox<Contract> choiceBox = new ChoiceBox<>(items);
      {
        choiceBox.setMaxHeight(maxHeight);
        choiceBox.setMaxWidth(maxWidth);
        choiceBox.valueProperty().addListener((obs, oldValue, newValue) -> {
          ObservableValue<Contract> value = getTableColumn().getCellObservableValue(getIndex());
          if (value instanceof WritableValue) {
            ((WritableValue<Contract>) value).setValue(newValue);
          }
        });
      }

      @Override
      protected void updateItem(Contract item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
          setText(null);
          setGraphic(null);
        } else {
          if (isEditing()) {
            setText(null);
            setGraphic(null);
          } else {
            choiceBox.setValue(item);
            setText(null);
            setGraphic(choiceBox);
          }
        }
      }
    };
  }
}
公共类ChoiceBoxTableCellFactory实现回调{
私人可观察清单项目;
私人双高;
专用双最大宽度;
公共选择BoxTableCellFactory(){
}
公共选择BoxTableCellFactory(
@NamedArg(“maxHeight”)双倍maxHeight,
@NamedArg(“maxWidth”)双maxWidth,
@名称代码(“项目”)可观察列表项目){
this.maxHeight=maxHeight;
this.maxWidth=maxWidth;
这个项目=项目;
}
@凌驾
公共TableCell调用(TableColumn参数){
返回新的TableCell(){
ChoiceBox ChoiceBox=新的ChoiceBox(项目);
{
choiceBox.setMaxHeight(maxHeight);
choiceBox.setMaxWidth(maxWidth);
choiceBox.valueProperty().addListener((obs、oldValue、newValue)->{
ObservableValue=getTableColumn().getCellObservableValue(getIndex());
if(可写值的值实例){
((WritableValue)值).setValue(newValue);
}
});
}
@凌驾
受保护的void updateItem(合同项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
设置图形(空);
}否则{
if(isEditing()){
setText(空);
设置图形(空);
}否则{
choiceBox.setValue(项目);
setText(空);
设置图形(选择框);
}
}
}
};
}
}
该表呈现良好,我在
选择框中看到了来自fxml的值。但是,当我单击任何值时,例如
M1
,就会调用
updateItem
方法,其中
item
参数将
userFriendlyName
设置为“”。选择
选择框
上的第一个选项

当我再次单击相同的
选择框
并选择任何选项时,不会再次调用
updateItem
方法,并且该值不会更改


我需要如何设置
选择框
以使用复杂类型
合同
作为选项,以及如何修复
选择框
上的状态更改?

问题是,在fxml中,我没有设置合同实现中使用的
名称
属性。对下拉列表的任何更改都将返回初始项

public class ChoiceBoxTableCellFactory<S, T> implements Callback<TableColumn<S, Contract>, TableCell<S, Contract>> {

  private ObservableList<Contract> items;
  private double maxHeight;
  private double maxWidth;

  public ChoiceBoxTableCellFactory() {
  }

  public ChoiceBoxTableCellFactory(
    @NamedArg("maxHeight") double maxHeight,
    @NamedArg("maxWidth") double maxWidth,
    @NamedArg("items") ObservableList<Contract> items) {
    this.maxHeight = maxHeight;
    this.maxWidth = maxWidth;
    this.items = items;
  }

  @Override
  public TableCell<S, Contract> call(TableColumn<S, Contract> param) {
    return new TableCell<S, Contract>() {
      ChoiceBox<Contract> choiceBox = new ChoiceBox<>(items);
      {
        choiceBox.setMaxHeight(maxHeight);
        choiceBox.setMaxWidth(maxWidth);
        choiceBox.valueProperty().addListener((obs, oldValue, newValue) -> {
          ObservableValue<Contract> value = getTableColumn().getCellObservableValue(getIndex());
          if (value instanceof WritableValue) {
            ((WritableValue<Contract>) value).setValue(newValue);
          }
        });
      }

      @Override
      protected void updateItem(Contract item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
          setText(null);
          setGraphic(null);
        } else {
          if (isEditing()) {
            setText(null);
            setGraphic(null);
          } else {
            choiceBox.setValue(item);
            setText(null);
            setGraphic(choiceBox);
          }
        }
      }
    };
  }
}