使用MouseEvent获取TableView单元格数据

时间:2014-12-07 20:11:43

标签: javafx mouseevent cell

我目前有一个带有自定义CellFactory的TableView。我试图找出当鼠标进入该单元格时如何获取单元格下的核心数据。

我将使用此数据填充另一个控制器中定义的标签。

我一直试图查看事件过滤器和事件处理程序,并怀疑答案与使用这些事件有关,但我还没有能够弄明白或找到答案。

请告诉我您希望看到的代码(如果有)。我甚至不确定在这一点上会有什么帮助。

1 个答案:

答案 0 :(得分:0)

向单元格注册鼠标侦听器以处理mouseEntered个事件,并在单元格上调用getItem()以获取数据:

TableView<MyRowType> table = ... ;
TableColumn<MyRowType, MyCellType> column = ... ;

column.setCellFactory ( c-> {
    TableCell<MyRowType, MyCellType> cell = new TableCell<MyRowType, MyCellType>() {
        @Override
        public void updateItem(MyCellType item, boolean empty) {
            super.updateItem(item, empty) ;
            // your implementation here...
        }
    };
    cell.setOnMouseEntered( e -> {
        MyCellType item = cell.getItem();
        if (item != null) {
            // do whatever you need with item
        }
    });
    return cell ;
});