JavaFXListView,获取列表中单元格的文本值
我试图在一个JavaFx应用程序上显示的ListView中获取所选单元格的文本 这样做的目的是修复我在编写应用程序时遇到的错误。当基础模型更改时,ListView中单元格的文本将无法正确更新。这在过去是行之有效的。 我正在尝试编写一个cucumber验收测试,这样如果它再次发生,bug就会被捕获 下面是此特定场景的步骤定义JavaFXListView,获取列表中单元格的文本值,java,listview,javafx,cucumber,Java,Listview,Javafx,Cucumber,我试图在一个JavaFx应用程序上显示的ListView中获取所选单元格的文本 这样做的目的是修复我在编写应用程序时遇到的错误。当基础模型更改时,ListView中单元格的文本将无法正确更新。这在过去是行之有效的。 我正在尝试编写一个cucumber验收测试,这样如果它再次发生,bug就会被捕获 下面是此特定场景的步骤定义 @Given("^I have selected an item from the list display$") public void I_have_selected_a
@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
上设置了单元格工厂。在后一种情况下,只需重构逻辑即可将显示文本转换为单独的方法:
ListView<MyModelObject> listView = ... ;
listView.setCellFactory(lv -> new ListCell<MyModelObject>() {
@Override
public void updateItem(MyModelObject item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(getDisplayText(item));
}
}
};
// ...
private String getDisplayText(MyModelObject object) {
// ...
return ... ;
}
(显然,如果您没有设置单元格工厂,您只需要
listView.getSelectionModel().getSelectedItem().toString()
)在某个地方,您必须在listView
上设置单元格工厂,以显示调用模型的toString()
方法的结果以外的内容。只需将该功能移到一个单独的方法中,然后调用它,并传递模型对象(您只需使用listView.getSelectionModel().getSelectedItem()
)即可。我特意不使用单元格工厂。不幸的是,这对我没有帮助。那么单元格中显示的值是多少?我已将Dave Fred的名称更改为Not Dave,并且它在listView中未更新。这是我正在修复的bug,我想写一个测试。请发布一个。如果模型中的数据与显示的数据不同,则说明您没有正确设置(尽管正确的方法是使用单元格工厂)。不幸的是,单元格中的文本不会更新。但是,编辑字段时,模型会更新。我也不使用细胞工厂listView.getSelectionModel().getSelectedItem.toString()将调用链接到单元格的对象的toString方法。不是细胞本身的文本。你说“我没有使用细胞工厂”是什么意思。列表视图中的单元格是如何以其他方式创建的?您可以使用ObservableList,它与控制器的初始值设定项ListView中的ListView相关联ListView.setItems(ObservableList)代码>然后您可以将项目添加到observableList中,它们将显示在listView中。在这种情况下,假设您没有显式设置单元格工厂,listView.getSelectionModel().getSelectedItem().toString()将为您提供列表视图中显示的文本。由于listView尚未刷新,因此显示的文本是旧文本。但是,调用listView.getSelectionModel().getSelectedItem().toString()
将获得listView更新后显示的文本。无论listView是否已更新,我都希望获取当前显示的文本。
MyModelObject item = listView.getSelectionModel().getSelectedItem();
String displayText = getDisplayText(item);