2015-11-14 27 views
0

跟進this問題。構建JavaFX上下文菜單項時查找模型詳細信息

我需要用行模型中的一些內容構造一個MenuItems。

row.getItem()爲null,因爲該行未在rowfactory回調中初始化。

當行模型已被填充時,是否有一種方法可以推遲創建上下文菜單?

例如,如果該行爲String屬性name =「foo」,則MenuItem應顯示「Add foo」。

+0

只是一個偵聽器添加到該行的'itemProperty'並更新菜單項時,它的變化。 –

+0

你有什麼可以指點我的例子嗎?回撥後創建上下文菜單嗎? –

回答

1

在我的回答Determine a JavaFX table row details when reusing tableview context menu的情況下,你會做

table.setRowFactory(t -> { 
    TableRow<Item> row = new TableRow<>(); 
    ContextMenu contextMenu = new ContextMenu(); 
    MenuItem item1 = new MenuItem(); 

    // ... 

    row.itemProperty().addListener((obs, oldItem, newItem) -> { 
     item1.setText(/* value depending on newItem... */); 
    }); 

    // ... 
} 
+0

非常感謝! –