javafx 自定义ListView的CellFactory后 显示数据时出现数据重复问题
时间: 2018-01-16来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
编译环境:
jdk 1.8.0_131
ide IDEA Community 2017.3
功能描述:
本人尝试编写一个这样的用户界面,BorderPane左侧为ListView,中心为一个Label和TextField外加一个名为Submit的Button。当在TextField中输入字符串,并点击Submit后,被输入的字符串及创建的时间,还有一个状态Pass将被显示在左侧的ListView中。
为了实现此显示功能,我重新设置了ListView的CellFactory。在CellFactory中返回一个内部类TitleCell继承了ListCell类,重写其updateItem方法。如下:
private class TitleCell extends ListCell<SimpleDocument> { @Override public void updateItem(SimpleDocument item, boolean empty){ super.updateItem(item, empty); if(!empty && item != null){ BorderPane cell = new BorderPane(); Text title = new Text(item.getTitle()); title.setFont(Font.font(14)); Text date = new Text(item.getDate().toString()); date.setFont(Font.font(10)); Text source = new Text(item.getStatus()); source.setFont(Font.font(10)); cell.setTop(title); cell.setLeft(date); cell.setRight(source); setGraphic(cell); } }
问题出现:
编译通过,执行程序。向TextField输入字符串"abc"点击Submit结果正常。如图:

再次向TextField输入字符串"def",点击Submit,问题出现。

如上图,出现显示了两次def的问题。
为什么会如此,怎样解决?还请各位大神不吝赐教。
附源代码链接: 代码

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行