2014-12-27 35 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

相關問題