簡體   English   中英

JavaFX 8 Custom TableCell實現呈現了三倍

[英]JavaFX 8 Custom TableCell implementation is rendering three times

我已經開發了一個簡單的自定義TableCell來啟用表中值的版本。 無論用戶是否在編輯該單元格,組件的行為都顯示為BigDecimalTextField ,它應始終啟用該版本。 該組件運行良好,只有一個奇怪的問題:呈現表時,顯示的是三行,而不是僅顯示一行:

渲染三遍

該組件的代碼是這樣的:

public class BigDecimalEditingCell extends TableCell {

    private BigDecimalField spinner = new BigDecimalField(new BigDecimal("0.00"));
    private ObjectProperty<BigDecimal> campoLigado = null;

    public BigDecimalEditingCell() {
        this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        createField();
    }

    private void createField() {
        spinner.setStepwidth(new BigDecimal("0.01"));
        spinner.setMinValue(new BigDecimal("0.00"));
        spinner.setFormat(NumberFormat.getInstance());
        spinner.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    }

    @Override
    public void updateItem(Object item, boolean empty) {
        super.updateItem(item, empty);
        criarBind();
        setGraphic(spinner);
        setText(null);
    }

    private void criarBind() {
        ObservableValue<BigDecimal> valor = getTableColumn().getCellObservableValue(getIndex());
        if (valor != null) {
            ObjectProperty<BigDecimal> propriedade = (ObjectProperty<BigDecimal>) valor;
            if (campoLigado == null) {
                spinner.numberProperty().bindBidirectional(propriedade);
                campoLigado = propriedade;
            } else if (campoLigado != propriedade) {
                spinner.numberProperty().unbindBidirectional(campoLigado);
                spinner.numberProperty().bindBidirectional(propriedade);
                campoLigado = propriedade;
            }
        }
    }    
}

如果我使用默認的TextFieldTableCell ,則表將正確呈現。 我還有另一個使用JavaFX的DatePicker的組件(像這樣),並且發生了相同的問題。 我做錯了什么?

添加

這是此組件的用法:

public class ControladorPainelFormaPagamento extends ControladorPainelSubmeter {

    @FXML
    private TableView<ParcelaBean> tabela;
    @FXML
    private TableColumn<ParcelaBean, LocalDate> colunaVencimento;
    @FXML
    private TableColumn<ParcelaBean, BigDecimal> colunaValor;
    private FormaPagamentoBean bean;

    .
    .
    .

    private void configurarColunaValor() {
        colunaValor.setCellValueFactory(new PropertyValueFactory<ParcelaBean, BigDecimal>("valor"));
        colunaValor.setCellFactory(new Callback<TableColumn<ParcelaBean, BigDecimal>, TableCell<ParcelaBean, BigDecimal>>() {
            @Override
            public TableCell<ParcelaBean, BigDecimal> call(TableColumn<ParcelaBean, BigDecimal> parcelaBeanStringTableColumn) {
                return new BigDecimalEditingCell();
            }
        });
    }

    private void configurarColunaVencimento() {
        colunaVencimento.setCellValueFactory(new PropertyValueFactory<ParcelaBean, LocalDate>("dataVencimento"));
    }

    public void carregar(ModoExibicao modoExibicao, FormaPagamentoBean formaPagamento) {
        this.bean=formaPagamento;
        tabela.setItems(bean.getParcelas());
        .
        .
        .
    }

    .
    .
    .

}

我已經使用調試功能(包括調試功能)檢查了表使用的列表中是否有多個bean。 每次只有一個。

我在同一系統中查看另一個表,並注意到了這一點:當表中沒有任何項時,將不顯示任何行。 空表

但是當您僅添加一行時,表格將呈現空白行,直到達到表格的高度(修復淺灰色的行):

行的表

因此,很明顯,多余的行是由TableView添加的,通過這種簡單的null檢查即可解決問題:

public class BigDecimalEditingCell extends TableCell {

    private BigDecimalField element = new BigDecimalField(new BigDecimal("0.00"));
    private ObjectProperty<BigDecimal> campoLigado = null;

    @Override
    protected void updateItem(Object item, boolean empty) {
        super.updateItem(item,empty);
        if (getIndex() < getTableView().getItems().size() && !empty) {
            createField();
            createBind();
            setGraphic(element);
            setText(null);
        } else {
            removeBind();
            setGraphic(null);
        }
    }

    .
    .
    .
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM