我想创建一个每行都有Buttons
的TableView
来删除特定行。因此,我创建了一个扩展TableCell
的类,其中包含一个按钮。所以我像这样设置列的CellFactory
:
clmRemoveButtons.setCellFactory(c -> new RemovingCell(clients));
一切正常。我可以删除行并且按钮显示正确,但有一个问题。整个列中有按钮。数据ObservableList
中有多少项并不重要。带有项目的行中的按钮可以正常工作,但是当我单击超出此范围的按钮时,会出现IndexOutOfBoundsException
(这是正确的,因为此时没有要删除的数据(。
所以我的问题是,我做错了什么,我怎样才能避免这个问题?
此致敬意
编辑:RemovingCell
代码(注意:HoverButton
是一个使用一些设置(大小等(扩展JavaFX Button
的控件,所以没有什么特别的。
public class RemovingCell extends TableCell<Client, Client> {
private HoverButton hb = new HoverButton(Color.RED);
public RemovingCell(ObservableList<Client> data) {
super();
setAlignment(Pos.CENTER);
try {
ImageView imgv = new ImageView(new Image(new FileInputStream(
"img/Remove.png")));
imgv.setFitWidth(15);
imgv.setPreserveRatio(true);
imgv.setSmooth(true);
hb.setGraphic(imgv);
hb.setTooltip(ControlFactory.getTooltip("Klient entfernen"));
hb.setOnAction(event -> {
data.remove(getTableRow().getIndex());
});
setGraphic(hb);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
表中所有数据所需的TableView
s用空单元格填充额外的空间。您的TableCell
需要检查单元格是否为空,然后再决定是否在其中包含按钮。您可以使用单元格emptyProperty()
上的侦听器或与其绑定来执行此操作:
public class RemovingCell extends TableCell<Client, Client> {
private HoverButton hb = new HoverButton(Color.RED);
public RemovingCell(ObservableList<Client> data) {
super();
setAlignment(Pos.CENTER);
try {
ImageView imgv = new ImageView(new Image(new FileInputStream(
"img/Remove.png")));
imgv.setFitWidth(15);
imgv.setPreserveRatio(true);
imgv.setSmooth(true);
hb.setGraphic(imgv);
hb.setTooltip(ControlFactory.getTooltip("Klient entfernen"));
hb.setOnAction(event -> {
data.remove(getTableRow().getIndex());
});
// conditionally set the graphic:
// setGraphic(hb);
emptyProperty().addListener( (obs, wasEmpty, isNowEmpty) -> {
if (isNowEmpty) {
setGraphic(null);
} else {
setGraphic(hb);
}
});
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
使用绑定您可以执行
graphicProperty().bind(Bindings.when(emptyProperty())
.then((Node)null)
.otherwise(hb));
而不是将侦听器添加到emptyProperty()
中。两者之间的选择只是风格问题。