我在javafx tableview中遇到以下问题,我的表视图连接到某个模型,正常的CRUD操作没有问题,我还添加了一个没有连接到任何数据模型的列,它只包含一个用户可以在其上的超链接单击,弹出提示他。
所有这些工作,它不起作用的是,当我点击超链接时我想传递行值,通常它的工作原理就像
tableview.getSelectionModel().getSelectedItem();
但现在它不起作用,因为我没有直接点击一个单元格,但我点击一个超链接,如果我先点击一行然后点击超链接,我会得到我突出显示的那一行。有没有办法在单击超链接时选择行,所以我不必先在同一行中单击行然后超链接。
public class RemoveCell<T> extends TableCell<T, Void> {
private final Hyperlink link;
private final Hyperlink link1;
private final HBox pane = new HBox();
public RemoveCell() {
link = new Hyperlink("Remove");
link1 = new Hyperlink("Edit");
pane.getChildren().addAll(link,link1);
link1.setOnAction(evt -> {
//lagerRet();
if(tableView.getSelectionModel().getSelectedItem()!=null) {
System.out.println("not null");
}
else {
System.out.println("null");
}
// remove row item from tableview
// ap.getChildren().removeAll();
//ap.getChildren().setAll(mcon.loadParent(FxmlView.CHART));
PopOver popsy = new PopOver();
try {
popsy.setContentNode(control.loadUni(FxmlView.POP));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//popsy.setContentNode(panemain);
popsy.headerAlwaysVisibleProperty().set(true);;
popsy.show(link);
});
link.setOnAction(evt -> {
// remove row item from tableview
System.out.println("a quick test");
});
}
@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : pane );
//setGraphic(empty ? null : link1 );
}
}
最后这是我如何填充列
testColumn.setCellFactory(tc -> new RemoveCell<>());
可以通过包含TableRow
的TableCell
访问行项目。
T item = getTableRow().getItem();
也可以使用TableView.items
获取TableCell.getIndex
中的索引,该索引允许删除而无需首先在列表中搜索项目。
int itemIndex = getIndex();
getTableView().getItems().remove(itemIndex);