JavaFx @FXML 分页注入到我的控制器 class 无法修改

JavaFx @FXML Pagination injected to my controller class can't be modified

我使用场景生成器设计了我的 fxml。分页默认有 10 页,并带有页码按钮。我想根据我的情况更改这些默认内容。

Here is what I have done in my controller:

  @FXML
    private Pagination pagination;
    ...


    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        pagination = new Pagination(7, 0);
        pagination.setPageFactory((Integer pageIndex)->createUserInfo(pageIndex));
        pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);

    }
    ...

private VBox createUserInfo(int pageIndex) {
    VBox box = new VBox();
    ImageView iv = new ImageView(images[pageIndex]);
    box.setAlignment(Pos.CENTER);
    Label desc = new Label("PAGE Number");
    box.getChildren().addAll(iv, desc);
    return box;
}

Here is my FXML :

<StackPane fx:id="stackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="538.0" prefWidth="747.0" style="-fx-background-color: #e8eaf6;" stylesheets="@../styles/inventory_fxml.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.inventory.controller.UserMange_fxmlController">
   <children>
      <Group nodeOrientation="LEFT_TO_RIGHT">
         <children>
            <VBox fx:id="vbox" prefHeight="483.0" prefWidth="685.0">
               <children>
                  <Pagination fx:id="pagination" prefHeight="352.0" prefWidth="685.0" stylesheets="@../styles/inventory_fxml.css" />

                 ....

但我仍然在 运行 应用程序时获得默认配置。我的代码有什么问题吗??提前致谢....

您正在创建一个新的 Pagination 然后对其进行配置,而不是配置您在 FXML 中定义的那个(即显示在 UI 中的那个)。

最重要的是,您永远不应将新对象分配给 @FXML-注释引用。

而不是

pagination = new Pagination(7, 0);

pagination.setPageCount(7);
pagination.setCurrentPageIndex(0);