在 Pgination JavaFX 中的项目上添加点击侦听器

Add a click listener on an item in a Pgination JavaFX

我正在尝试将侦听器添加到提取所选项目的分页项目。

我有一个 class PaginationController,其中包含我的 Paginaton,但该项目在另一个 class 上: 项目控制器。

我希望能够在用户在 PaginationController 中单击某个项目时提取该项目的信息 Class。

这是我的代码:

@Override
public void initialize(URL url, ResourceBundle rb) {
   refreshNodes() ; 
   p.setPageFactory(new Callback<Integer, Node>() {
        @Override
        public Node call(Integer index) {

            StackPane page = new StackPane();

            GridPane grid = new GridPane();

            grid.setHgap(20);
            grid.setVgap(20);
            grid.setPadding(new Insets(20, 20, 20, 20));

            int total = 10 ; 
            int rows = 2 ; 
            int cols = 2 ; 

            int offset = rows * cols * index;

            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {

                    offset++;

                    if (offset > total)
                        break;

                    StackPane container = new StackPane();
                    container.setStyle("-fx-background-color:white");

                    
                    container.getChildren().add(nodes[offset]);

                    GridPane.setRowIndex(container, row);
                    GridPane.setColumnIndex(container, col);
                    GridPane.setHgrow(container, Priority.ALWAYS);
                    GridPane.setVgrow(container, Priority.ALWAYS);

                    grid.getChildren().add(container);
                }
            }

            page.getChildren().add(grid);

            return page;
        }
    });
}

最好的方法是使 GridPane 中的节点可点击,并为这些点击设置事件处理程序,将数据导出回分页控制器。不要将节点视为数据,而应将它们视为可以执行某些操作的活动元素。此外,尽可能不要重建您的布局以响应点击和用户操作。您需要做的就是重新填充您的 GridPane,您不需要每页创建一个全新的:

public class PaginationSample 扩展应用程序 {

public static void main(String[] args) {
    launch(args);
}

Node[] nodes = new Node[100];
int index = 0;

@Override
public void start(Stage primaryStage) {
    Scene scene = new Scene(new MainScreen(), 200, 200);
    primaryStage.setScene(scene);
    Consumer<String> nodeConsumer = nodeData -> System.out.println("Node data: " + nodeData);
    for (int x = 0; x < 100; x++) {
        nodes[x] = new NodeThing("Node " + x, nodeConsumer);
    }
    primaryStage.show();
}

class MainScreen extends BorderPane {

    MainScreen() {
        PageNode pageNode = new PageNode();
        setCenter(pageNode);
        Button nextButton = new Button("Next");
        nextButton.setOnAction(evt -> pageNode.loadPage(index++));
        Button prevButton = new Button("Previous");
        prevButton.setOnAction(evt -> pageNode.loadPage(index--));
        setBottom(new HBox(30, prevButton, nextButton));
    }
}

class NodeThing extends VBox {
    NodeThing(String nodeData, Consumer<String> dataReciever) {
        Button button = new Button("Button");
        button.setOnAction(evt -> dataReciever.accept(nodeData));
        getChildren().addAll(new Text(nodeData), button);
    }
}

class PageNode extends GridPane {

    PageNode() {
        setHgap(20);
        setVgap(20);
        setPadding(new Insets(20));
    }

    void loadPage(int index) {
        getChildren().clear();
        int rows = 2;
        int cols = 2;

        int offset = rows * cols * index;

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (offset++ >= nodes.length) {
                    break;
                }
                StackPane container = new StackPane();
                container.setStyle("-fx-background-color:white");
                container.getChildren().add(nodes[offset]);
                add(container, col, row);
                GridPane.setHgrow(container, Priority.ALWAYS);
                GridPane.setVgrow(container, Priority.ALWAYS);
            }
        }
    }
}

}