如何在javafx中将TableCell控件与底层行模型连接?

如何在javafx中将TableCell控件与底层行模型连接?,javafx,tableview,spinner,fxml,Javafx,Tableview,Spinner,Fxml,我使用的是由我的模型填充的TableView 我想在一列中使用微调器控件 我可以在所需列的单元格中创建微调器, 但我正在努力将微调器值绑定到models属性 这是fxml <ScrollPane> <content> <TableView prefHeight="525.0" prefWidth="814.0"> <columns> <TableColumn prefWidth="75.0

我使用的是由我的模型填充的TableView

我想在一列中使用微调器控件

我可以在所需列的单元格中创建微调器, 但我正在努力将微调器值绑定到models属性

这是fxml

<ScrollPane>
   <content>
      <TableView prefHeight="525.0" prefWidth="814.0">
        <columns>
          <TableColumn prefWidth="75.0">
            <cellValueFactory><PropertyValueFactory property="state"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Side">
            <cellValueFactory><PropertyValueFactory property="side"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Source">
            <cellValueFactory><PropertyValueFactory property="sourceContract"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Reference">
            <cellValueFactory><PropertyValueFactory property="referenceContract"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Destination">
            <cellValueFactory><PropertyValueFactory property="destinationContract"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Margin" editable="true">
            <cellFactory><SpinnerTableCellFactory /></cellFactory>
            <cellValueFactory><PropertyValueFactory property="margin"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Bot">
            <cellValueFactory><PropertyValueFactory property="bot"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Price">
            <cellValueFactory><PropertyValueFactory property="price"/></cellValueFactory>
          </TableColumn>
          <TableColumn prefWidth="75.0" text="Volume">
            <cellValueFactory><PropertyValueFactory property="volume"/></cellValueFactory>
          </TableColumn>
        </columns>
        <items>
          <FXCollections fx:factory="observableArrayList">
            <GridRowModel state="false" side="BID" sourceContract="s01" referenceContract="" destinationContract="d01" margin="0" bot="MinMax" price="15.125" volume="0" />
            <GridRowModel state="false" side="ASK" sourceContract="s02" referenceContract="" destinationContract="d02" margin="0" bot="MinMax" price="15.125" volume="0" />
          </FXCollections>
        </items>
      </TableView>
   </content>
</ScrollPane>

加载fxml时,将使用fxml中
margin
属性的默认值调用
updateItem
方法。我可以看到并使用单元格中的微调器。但是如何将任何新微调器值传递回GridRowModel对象中的margin属性?

只需使用微调器的
valueProperty()
注册一个侦听器即可:

然后,您可以将
fx:id
添加到您的工厂:

<cellFactory><SpinnerTableCellFactory fx:id="marginCellFactory" /></cellFactory>

在控制器中执行以下操作:

@FXML
private SpinnerTableCellFactory<GridRowModel, Double> marginCellFactory ;

public void initialize() {
    marginCellFactory.setUpdater(GridRowModel::setMargin);
}
@FXML
私人喷丝板工厂;马金塞尔工厂;
公共无效初始化(){
setUpdater(GridRowModel::setMargin);
}

只需使用微调器的
valueProperty()注册一个侦听器即可。

然后,您可以将
fx:id
添加到您的工厂:

<cellFactory><SpinnerTableCellFactory fx:id="marginCellFactory" /></cellFactory>

在控制器中执行以下操作:

@FXML
private SpinnerTableCellFactory<GridRowModel, Double> marginCellFactory ;

public void initialize() {
    marginCellFactory.setUpdater(GridRowModel::setMargin);
}
@FXML
私人喷丝板工厂;马金塞尔工厂;
公共无效初始化(){
setUpdater(GridRowModel::setMargin);
}
@FXML
private SpinnerTableCellFactory<GridRowModel, Double> marginCellFactory ;

public void initialize() {
    marginCellFactory.setUpdater(GridRowModel::setMargin);
}