Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击或悬停等获取GridPane对象中的单元格位置。javafx_Javafx_Gridpane_Onhover - Fatal编程技术网

单击或悬停等获取GridPane对象中的单元格位置。javafx

单击或悬停等获取GridPane对象中的单元格位置。javafx,javafx,gridpane,onhover,Javafx,Gridpane,Onhover,我有一个用于创建日历的GridPane。 当我将鼠标悬停在任何日历单元格上时,我希望获得该特定单元格的位置 最终目标是使用该单元的位置访问其子单元并对其执行操作。我想做的一件具体事情是创建一个自定义(非JavaFX)工具提示,并将其在场景图中的位置移动到悬停单元格的位置 在我看来,知道鼠标悬停在哪个单元格上是没有意义的。您应该将每个单元格的内容转换成某种可重用的控件 例如: public class CellControl extends BorderPane { // Your UI

我有一个用于创建日历的GridPane。 当我将鼠标悬停在任何日历单元格上时,我希望获得该特定单元格的位置

最终目标是使用该单元的位置访问其子单元并对其执行操作。我想做的一件具体事情是创建一个自定义(非JavaFX)工具提示,并将其在场景图中的位置移动到悬停单元格的位置


在我看来,知道鼠标悬停在哪个单元格上是没有意义的。您应该将每个单元格的内容转换成某种可重用的控件

例如:

public class CellControl extends BorderPane {
    // Your UI logic

    // Getters for data
    public final LocalDate getDate() {
        return date;
    }
}

ObservableList<CellControl> cells = FXCollections.observableArrayList(cell -> new Observable[] {cell.hoverProperty()});

ObjectProperty<CellControl> hoveredCell = new SimpleObjectProperty<CellControl>() {
    @Override protected void invalidated() {
        // Do something
    }
};

hoveredCell.bind(Bindings.createObjectBinding(
        () -> cells.stream().filter(CellControl::isHover).findAny().orElse(null)
    , cells));
公共类CellControl扩展了BorderPane{
//您的UI逻辑
//数据获取器
公共最终LocalDate getDate(){
返回日期;
}
}
ObservableList cells=FXCollections.observableArrayList(cell->new Observable[]{cell.hoverProperty()});
ObjectProperty hoveredCell=新的SimpleObjectProperty(){
@覆盖受保护的无效(){
//做点什么
}
};
hoveredCell.bind(Bindings.createObjectBinding(
()->cells.stream().filter(CellControl::isHover).findAny().orElse(null)
,细胞);

GridPane.getRowIndex
GridPane.getColumnIndex
可用于确定行/列

假设您不移动单元格,那么使索引/单元格本身可供侦听器使用通常更简单:

@Override
public void start(Stage primaryStage) {
    GridPane root = new GridPane();

    int row = 0;
    int column = 0;
    for (int i = 0; i < 30; i++) {
        Text text = new Text(Integer.toString(i + 1));
        StackPane cell = new StackPane(text);
        cell.setStyle("-fx-background-color: lightblue; -fx-border-width: 2; -fx-border-color: black; -fx-border-style: solid");
        cell.setMinSize(50, 50);
        root.add(cell, column, row);

        final int rowFinal = row;
        final int columnFinal = column;
        cell.setOnMouseEntered(evt -> {
            System.out.format("column=%d; row=%d; cell=%s\n", columnFinal, rowFinal, cell);
            text.setText(text.getText() + "*");
        });
        cell.setOnMouseExited(evt -> {
            String s = text.getText();
            text.setText(s.substring(0, s.length() - 1));
        });

        column++;
        if (column == 7) {
            column = 0;
            row++;
        }
    }

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}
@覆盖
公共无效开始(阶段primaryStage){
GridPane root=新的GridPane();
int行=0;
int列=0;
对于(int i=0;i<30;i++){
Text Text=新文本(Integer.toString(i+1));
StackPane单元格=新的StackPane(文本);
cell.setStyle(“-fx背景颜色:浅蓝色;-fx边框宽度:2;-fx边框颜色:黑色;-fx边框样式:纯色”);
cell.setMinSize(50,50);
root.add(单元格、列、行);
final int rowFinal=行;
final int column final=列;
cell.SetonMouseCentered(evt->{
System.out.format(“列=%d;行=%d;单元格=%s\n”,columnFinal,rowFinal,单元格);
text.setText(text.getText()+“*”);
});
cell.setOnMouseExited(evt->{
字符串s=text.getText();
setText(s.substring(0,s.length()-1));
});
列++;
如果(列==7){
列=0;
行++;
}
}
场景=新场景(根);
初级阶段。场景(场景);
primaryStage.show();
}
Post a。。