在FXML中为TableView设置SelectionModel

bas*_*aad 2 javafx selectionmodel tableview javafx-8

我想从FXML设置TableView的SelectionModel,但我找不到如何做到这一点.我已经尝试过以下方法:

1.只需将其设置为TableView的属性:

<TableView selectionModel="MULTIPLE">
Run Code Online (Sandbox Code Playgroud)

2.设置与ListView相同的属性(请参阅:https://community.oracle.com/thread/2315611?start = 0&tstart = 0):

<TableView multiSelect="true">
Run Code Online (Sandbox Code Playgroud)

3.以不同的方式设置属性:

<TableView>
    <selectionModel>
        <TableView fx:constant="MULTIPLE" />
    </selectionModel>
</TableView>
Run Code Online (Sandbox Code Playgroud)

4.另一个版本:

<TableView>
    <selectionModel>
        <SelectionModel fx:constant="MULTIPLE" />
    </selectionModel>
</TableView>
Run Code Online (Sandbox Code Playgroud)

5.选择模型(不同):

<TableView>
    <selectionModel>
        <SelectionModel selectionModel="MULTIPLE" />
    </selectionModel>
</TableView>
Run Code Online (Sandbox Code Playgroud)

这些都不起作用.

任何帮助是极大的赞赏!

Jos*_*eda 6

如果可以在FXML上这应该是这样的:

<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
    <columns>
      <TableColumn prefWidth="75.0" text="C1" />
    </columns>
    <selectionModel>
        <SelectionMode fx:constant="MULTIPLE"/>
    </selectionModel>
</TableView>
Run Code Online (Sandbox Code Playgroud)

不幸的是,当你运行它时会遇到异常:

java.lang.IllegalArgumentException: Unable to coerce SINGLE to class javafx.scene.control.TableView$TableViewSelectionModel.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:495)
Run Code Online (Sandbox Code Playgroud)

这是发生因为豆适配器试图本能地在课堂上找到javafx.scene.control.TableView$TableViewSelectionModelvalueOfjavafx.scene.control.SelectionMode.MULTIPLE,但它没有找到它.

还有一个悬而未决的JIRA票这个位置.

基于该报告,我发现的唯一可行解决方案是使用脚本功能:

...
<?language javascript?>

    <TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
        <columns >
          <TableColumn fx:id="col" prefWidth="75.0" text="C1" />
        </columns>
    </TableView>
    <fx:script>          
          table.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE);
    </fx:script> 
Run Code Online (Sandbox Code Playgroud)

这与通过代码执行相同...