滚动以操作TableView,删除在表中创建的TextField的内容

时间:2014-10-15 13:37:26

标签: java javafx javafx-2

我将数字放在Mirror列中的文本字段中,但当我处理滚动条(滚动表格)时,文本字段的内容将被删除。有没有解决这个问题的方法?

创建我的表的代码:

@SuppressWarnings({ "unchecked", "rawtypes" })
private void loadTabMirror() {


    TableColumn request = new TableColumn("Pedido");
    request.setCellValueFactory(new PropertyValueFactory<MirrorVo, String>("request"));
    request.setMinWidth(150);

    TableColumn applicant = new TableColumn("Requerente");
    applicant.setCellValueFactory(new PropertyValueFactory<MirrorVo, String>("name"));
    applicant.setMinWidth(400);


    TableColumn numberRg = new TableColumn("N° RG");
    numberRg.setCellValueFactory(new PropertyValueFactory<MirrorVo, String>("rg"));
    numberRg.setMinWidth(80);

    Callback<TableColumn<MirrorVo, String>, TableCell<MirrorVo, String>> txtMirrorCallBack = 
            new Callback<TableColumn<MirrorVo, String>, TableCell<MirrorVo, String>>() {

        @Override
        public TableCell call(final TableColumn param) {
            final TableCell cell = new TableCell() {

                @Override
                public void updateItem(Object item, boolean empty) {
                    super.updateItem(item, empty);

                    if (empty) {
                        setText(null);
                        setGraphic(null);
                    } else if(item == null){
                        TextField textField = loadRuleTextField(param,getIndex()); 
                        textFieldMirrors.add(textField);
                        setGraphic(textField);
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

                    } 
                }
            };
            return cell;
        }
    };

    TableColumn mirror = new TableColumn("Espelho"); 
    mirror.setCellValueFactory(new PropertyValueFactory<MirrorVo, Integer>("mirror"));
    mirror.setMinWidth(100);
    mirror.setCellFactory(txtMirrorCallBack);

    Callback<TableColumn<MirrorVo, String>, TableCell<MirrorVo, String>> imgMirrorInfo = 
            new Callback<TableColumn<MirrorVo, String>, TableCell<MirrorVo, String>>() {

                @Override
                public TableCell<MirrorVo, String> call(
                        TableColumn<MirrorVo, String> param) {

                    final TableCell cell = new TableCell() {

                        @Override
                        public void updateItem(Object item, boolean empty) {
                            super.updateItem(item, empty);
                            if (empty) {
                                setText(null);
                                setGraphic(null);
                            } else {
                                ImageView imgViewInfo = new ImageView(new Image("resources/img/info.png"));
                                imgViewInfo.setFitWidth(32);
                                imgViewInfo.setFitHeight(32);
                                imgViewInfo.setVisible(false);
                                imgViewInfos.put(getIndex(), imgViewInfo);
                                setGraphic(imgViewInfo);
                                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                            }
                        }
                    };
                    return cell;
                }

    };

    TableColumn info = new TableColumn("Status");
    info.setMinWidth(40);
    info.setCellFactory(imgMirrorInfo);

    Callback<TableColumn<CivilRecord, String>, TableCell<CivilRecord, String>> btnInvalidateCallBack = addActionBtnInvalidate();

    TableColumn validation = new TableColumn(" ");
    validation.setCellValueFactory(new PropertyValueFactory<CivilRecord, String>("dateRegisterLot"));
    validation.setMinWidth(100);
    validation.setCellFactory(btnInvalidateCallBack);


    Callback<TableColumn<CivilRecord, String>, TableCell<CivilRecord, String>> btnAutoGenerateCallBack = addActionBtnAutoGenerate();

    TableColumn autoGenerate = new TableColumn(" ");
    autoGenerate.setCellValueFactory(new PropertyValueFactory<CivilRecord, String>("dateRegisterLot"));
    autoGenerate.setMinWidth(100);
    autoGenerate.setCellFactory(btnAutoGenerateCallBack);

    tabMirror.getColumns().addAll(request,applicant,numberRg,mirror,info,autoGenerate, validation);
}

1 个答案:

答案 0 :(得分:0)

TableCell列的mirror永远不会实际处理项目具有内容的情况(即项目不为空)。

滚动时,可能发生的事情是,从屏幕的可见部分滚动的TableCell被更新为空单元格,然后它们会在某些时候重新用于显示的新项目。因此,updateItem()方法首先使用empty=true调用(将文本和图形设置为null),稍后使用empty=falseitem作为项目进行调用要显示。由于您的updateItem方法根本不处理此情况,因此单元格的文本和图形保持为空。

您需要更改updateItem方法,以便它处理所有可能的情况:

  • empty == true(您已经拥有)
  • empty == false && item == null(您已经拥有)
  • empty == false && item != null(你不办理)