我想在JavaFx應用程序上顯示所選單元格的ListView中的文本。JavaFx ListView,獲取列表中的單元格的文本值
這樣做的目的是解決我在編寫應用程序時遇到的錯誤。當底層模型更改時,ListView中單元格的文本將無法正確更新。過去曾奏效。 我想寫一個黃瓜驗收測試,以便如果它再次發生,錯誤將被捕獲。
下面是這個特定場景的stepdefs。
@Given("^I have selected an item from the list display$")
public void I_have_selected_an_item_from_the_list_display() throws Throwable {
ListView displayList = (ListView) primaryStage.getScene().lookup("#displayList");
displayList.getSelectionModel().select(0);
}
@When("^I edit the items short name$")
public void I_edit_the_items_short_name() throws Throwable {
fx.clickOn("#projectTextFieldShortName").type(KeyCode.A);
fx.clickOn("#textFieldLongName");
}
@Then("^the short name is updated in the list display$")
public void the_short_name_is_updated_in_the_list_display() throws Throwable {
ListView displayList = (ListView) primaryStage.getScene().lookup("#displayList");
String name = "";
// This gets me close, In the debuger the cell property contains the cell I need, with the text
Object test = displayList.getChildrenUnmodifiable().get(0);
//This will get the actual model object rather than the text of the cell, which is not what I want.
Object test2 = displayList.getSelectionModel().getSelectedItem();
assertTrue(Objects.equals("Testinga", name));
}
我已經瀏覽了ListView JavaDoc,並找不到任何方法可以讓我得到單元格的文本。
某處你必須設置在'ListView'電池工廠,以顯示比調用模型的'的toString()'方法的結果以外的東西。只需將該功能移出一個單獨的方法,然後調用它,傳遞模型對象(您只需使用listView.getSelectionModel()。getSelectedItem()')即可獲得該模型對象。 –
我沒有使用細胞工廠。所以這不利於我。 – Awarua
那麼單元格中顯示的值是多少? –