[英]Add a click listener on an item in a Pgination JavaFX
我正在尝试将侦听器添加到提取所选项目的分页项目中。
我有一个 class PaginationController,其中包含我的 Paginaton,但该项目位于另一个 class:ItemController。
当用户在 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 中的节点可点击,并为那些将数据导出回您的分页 controller 的点击设置一个 EventHandler。 不要将节点视为数据,而应将它们视为可以做事的活动元素。 此外,尽可能不要重建布局以响应点击和用户操作。 您需要做的就是重新填充您的 GridPane,您不需要为每个页面创建一个全新的:
公共 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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.