如何在TableView JavaFX中获取TableCell的X和Y坐标? [英] How to get the X and Y coordinate of TableCell in the TableView JavaFX?
问题描述
我的TableView中有一个ComboBoxTableCell,当有人点击ComboBoxTableCell时,我希望另一个Pane将在ComboBoxTableCell下面显示一个项目列表。我试图获得ComboBoxTableCell的坐标,但没有运气。有没有办法获得坐标?或者你有更好的方法吗?下面是Sample UI的图片,我想完成。谢谢。
I have a ComboBoxTableCell in my TableView, when someone click the ComboBoxTableCell, I want that another Pane will display below the ComboBoxTableCell with a list of items. I tried to get the coordinate of the ComboBoxTableCell but no luck. Is there any way to get the coordinate? Or Do you have any better way to do this? Below is the image of Sample UI, I want to be done. Thanks.
这是我初始化TableView的方法
Here is my method to initialize the TableView
private void initOrderTable(){
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);
}
推荐答案
你可以获取相对于场景
的单元格坐标
You can get the coordinates of the cell relative to the Scene
with
Point2D location = ct.localToScene(0, 0);
或相对于屏幕
Point2D location = ct.localToScreen(0, 0);
这些将为您提供单元格左上角的坐标。你可以做到
Those will give you the coordinates of the top left of the cell. You can do
Point2D location = ct.localToScene(0, ct.getHeight());
获取相对于场景的左下角坐标
等。显然,当你需要显示窗格时,你需要在要调用的单元格上设置一个监听器。
to get the coordinates of the bottom left relative to the Scene
, etc. Obviously you will need to set a listener on the cell to be invoked when you need to show the pane.
这篇关于如何在TableView JavaFX中获取TableCell的X和Y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!