在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ javafx 自定義ListView的CellFactory后 顯示數(shù)據(jù)時出現(xiàn)數(shù)

javafx 自定義ListView的CellFactory后 顯示數(shù)據(jù)時出現(xiàn)數(shù)據(jù)重復(fù)問題

編譯環(huán)境:
jdk 1.8.0_131
ide IDEA Community 2017.3
功能描述:
本人嘗試編寫一個這樣的用戶界面,BorderPane左側(cè)為ListView,中心為一個Label和TextField外加一個名為Submit的Button。當(dāng)在TextField中輸入字符串,并點擊Submit后,被輸入的字符串及創(chuàng)建的時間,還有一個狀態(tài)Pass將被顯示在左側(cè)的ListView中。
為了實現(xiàn)此顯示功能,我重新設(shè)置了ListView的CellFactory。在CellFactory中返回一個內(nèi)部類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);
            }
        }

問題出現(xiàn):
編譯通過,執(zhí)行程序。向TextField輸入字符串"abc"點擊Submit結(jié)果正常。如圖:
圖片描述

再次向TextField輸入字符串"def",點擊Submit,問題出現(xiàn)。
圖片描述

如上圖,出現(xiàn)顯示了兩次def的問題。
為什么會如此,怎樣解決?還請各位大神不吝賜教。
附源代碼鏈接:代碼

回答
編輯回答
巫婆

@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);
        }else if(empty){
            setText(null);
            setGraphic(null);
        }
    }
2017年6月13日 18:57