[英]JavaFX 8 Custom TableCell implementation is rendering three times
I've developed a simple custom TableCell to enable the edition of the values in a table. 我已经开发了一个简单的自定义TableCell来启用表中值的版本。 The behaviour of the component is show a BigDecimalTextField no matter if the user is editing or not that cell, it should enable the edition all the time.
无论用户是否在编辑该单元格,组件的行为都显示为BigDecimalTextField ,它应始终启用该版本。 The component is working fine, there is only a strange problem: when the table is rendered, instead of show only a single line, three lines are shown:
该组件运行良好,只有一个奇怪的问题:呈现表时,显示的是三行,而不是仅显示一行:
The code of the component is this: 该组件的代码是这样的:
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;
}
}
}
}
If I use the default TextFieldTableCell , the table is rendered correctly. 如果我使用默认的TextFieldTableCell ,则表将正确呈现。 I have another component (like this) that uses JavaFX's DatePicker and the same problem happens.
我还有另一个使用JavaFX的DatePicker的组件(像这样),并且发生了相同的问题。 What I'm doing wrong?
我做错了什么?
ADDED
添加
Here is the usage of this component: 这是此组件的用法:
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());
.
.
.
}
.
.
.
}
I've checked, inclusive using debug, if there was more than one bean in the list used by the table. 我已经使用调试功能(包括调试功能)检查了表使用的列表中是否有多个bean。 Every time only one was there.
每次只有一个。
I was looking at another table in the same system and noticed this: when there is no item in the table, no one row is rendered. 我在同一系统中查看另一个表,并注意到了这一点:当表中没有任何项时,将不显示任何行。
but when you add just one row, the table renders empty row until reach the height of the table (repair the light grey rows): 但是当您仅添加一行时,表格将呈现空白行,直到达到表格的高度(修复浅灰色的行):
So, it was obvious that the extra rows were added by TableView, this way a simple null check solve the problem: 因此,很明显,多余的行是由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.