2014-12-27 74 views
1

我想从FXML中设置TableView的SelectionModel,但我找不到如何执行此操作。我已经试过如下:在FXML中为TableView设置SelectionModel

1.Just将其设置为TableView中的属性:

<TableView selectionModel="MULTIPLE"> 

2.将财产一样的ListView作品(见:https://community.oracle.com/thread/2315611?start=0&tstart=0):

<TableView multiSelect="true"> 

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

<TableView> 
    <selectionModel> 
     <TableView fx:constant="MULTIPLE" /> 
    </selectionModel> 
</TableView> 

4.Another版本:

<TableView> 
    <selectionModel> 
     <SelectionModel fx:constant="MULTIPLE" /> 
    </selectionModel> 
</TableView> 

5.Selection模型(不同的):

<TableView> 
    <selectionModel> 
     <SelectionModel selectionModel="MULTIPLE" /> 
    </selectionModel> 
</TableView> 

这一切都不工作。

任何帮助,非常感谢!

回答

5

它应该是可能的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> 

不幸的是,当你运行它,你会得到一个异常:

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

发生这种情况是因为豆适配器试图本能在类别javafx.scene.control.TableView$TableViewSelectionModel中找到javafx.scene.control.SelectionMode.MULTIPLEvalueOf,但它找不到它。

有这个here未解决的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> 

这是由代码做同样的...

+0

谢谢您的回答何塞佩雷达!在解决问题之前,我只会在代码中设置该部分。 – bashoogzaad 2015-01-04 14:00:35

+0

或者在控制器的初始化方法中执行此操作。 – keiki 2015-01-10 20:39:27