如何在TableView JavaFX中获取TableCell的X和Y坐标?

时间:2014-11-22 06:29:56

标签: javafx tableview

我的TableView中有一个ComboBoxTableCell,当有人点击ComboBoxTableCell时,我希望另一个Pane将在ComboBoxTableCell下面显示一个项目列表。我试图获得ComboBoxTableCell的坐标,但没有运气。有没有办法获得坐标?或者你有更好的方法吗?下面是Sample UI的图片,我想完成。感谢。

This is the sample UI I want to be done.

这是我初始化TableView的方法

private void initOrderTable(){

    this.orderTable.setEditable(true);
    TableColumn nameCol = new TableColumn("Name");
    nameCol.setMinWidth(100);

    nameCol.setCellValueFactory(
    new PropertyValueFactory<>("name"));
    nameCol.setCellFactory(new Callback<TableColumn<Product, String>,ComboBoxTableCell<Product,String>>() {

        @Override
        public ComboBoxTableCell<Product, String> call(TableColumn<Product, String> param) {

           ComboBoxTableCell ct= new ComboBoxTableCell<>();
           ct.setComboBoxEditable(true);

           return ct;
        }
});


    this.orderTable.getItems().addAll("Sample Row");
    this.orderTable.getColumns().addAll(nameCol);

}

1 个答案:

答案 0 :(得分:1)

您可以使用

获取相对于Scene的单元格坐标
Point2D location = ct.localToScene(0, 0);

或相对于屏幕

Point2D location = ct.localToScreen(0, 0);

这些将为您提供单元格左上角的坐标。你可以做到

Point2D location = ct.localToScene(0, ct.getHeight());

获取左下角相对于Scene等的坐标。当你需要显示窗格时,显然你需要在要调用的单元格上设置一个监听器。