2

I am using JavaFX in my project (also Spring, Hibernate e.t.c.).

I am trying to create Pagination for TableView. I found many reasonable solutions, but all off them are controller-based solutions. The best of them in this solution.

But in my situation - I am using multiple fxml files which were created in SceneBuilder with all huge design options and which contains the TabPane and for one Tab I have one FXML-file and one controller for it.

I have one basic file which imports others, and in my Main class I am loading it:

enter image description here

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="592.0" prefWidth="920.0" xmlns="http://javafx.com/javafx/8"
            fx:controller="com.varinsia.statistical.controller.GenericController">
    <children>
        <TabPane layoutX="3.0" layoutY="50.0" prefHeight="542.0" prefWidth="914.0" tabClosingPolicy="UNAVAILABLE">
            <tabs>
                <Tab text="Statistics">
                    <content>
                        <fx:include fx:id="statistics" source="/fxml/child/statistics.fxml"/>
                    </content>
                </Tab>
                <Tab text="Shedule">
                    <content>
                        <fx:include fx:id="shedule" source="/fxml/child/shedule.fxml"/>
                    </content>
                </Tab>
                <Tab text="Ponab devices">
                    <content>
                        <fx:include fx:id="ponabDevices" source="/fxml/child/ponabDevices.fxml"/>
                    </content>
                </Tab>
                <Tab text="Als devices">
                    <content>
                        <fx:include fx:id="alsDevices" source="/fxml/child/alsDevices.fxml"/>
                    </content>
                </Tab>
                <Tab text="Search">
                    <content>
                        <fx:include fx:id="search" source="/fxml/child/search.fxml"/>
                    </content>
                </Tab>
                <Tab text="Settings">
                    <content>
                        <fx:include fx:id="settings" source="/fxml/child/settings.fxml"/>
                    </content>
                </Tab>
            </tabs>
        </TabPane>
        <Label layoutX="127.0" layoutY="9.0" text="..."
               underline="true">
            <font>
                <Font size="18.0"/>
            </font>
        </Label>
    </children>
</AnchorPane>

In my controller class for my statistics.fxml I am creating the TableView and columns:

@FXML
    public TableView<StatisticsRemarkTableDto> statisticsTableView;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> objectColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> noteColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> stageColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> dateColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> vagonColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, String> repeatColumn;
    @FXML
    public TableColumn<StatisticsRemarkTableDto, Integer> remarkIdColumn;

And all other things like in this solution. Information from the entity is added to my table and everything works fine. But!

Problem starts when i am trying to add Pagination, because I can't understand how I should do this. My Main method looks like this:

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring/application-context.xml");
        context.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);
        primaryStage.setTitle(Constants.MAIN_TITLE);
        primaryStage.setScene(new Scene((Parent) context.getBean(SpringFXMLLoader.class).load(Constants.FXML_PATH), 914, 542));
        primaryStage.show();
    }
}

In all TableView-pagination examples Pagination were added in the Main class, like in the code from other valid and checked example:

private Node createPage(int pageIndex) {

        int fromIndex = pageIndex * rowsPerPage;
        int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
        table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));

        return new BorderPane(table);
    }

    @Override
    public void start(final Stage stage) throws Exception {

        Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
        pagination.setPageFactory(this::createPage);

        Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
        stage.setScene(scene);
        stage.setTitle("Table pager");
        stage.show();
    }

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

Is there some way to pass this issue and to add Pagination to my TableView? I'll be very glad to any answers on this subject, the problem does not suffer the first day.

RESOLVED!:

Thanks to James_D and his solution. I have tried this variant and it worked fine. My TableView is now with pagination.

I have added to my statistical.fxml:

<Pagination fx:id="statisticsTableViewPagination" layoutX="2.0" layoutY="188.0" prefHeight="275.0"
                prefWidth="912.0">
        <fx:define>
            <TableView fx:id="statisticsTableView" layoutX="2.0" layoutY="188.0" prefHeight="301.0" prefWidth="912.0">
                <placeholder>
                    <Label text="No search results found."/>
                </placeholder>
                <columns>
                    <TableColumn fx:id="objectColumn" prefWidth="131.0" text="Object"/>
                    <TableColumn fx:id="noteColumn" minWidth="0.0" prefWidth="167.0" text="Remark"/>
                    <TableColumn fx:id="stageColumn" prefWidth="282.0" text="Stage"/>
                    <TableColumn fx:id="dateColumn" prefWidth="72.0" text="Date"/>
                    <TableColumn fx:id="vagonColumn" prefWidth="133.0" text="Laboratory"/>
                    <TableColumn fx:id="repeatColumn" prefWidth="125.0" text="Repeats"/>
                    <TableColumn fx:id="remarkIdColumn" minWidth="9.0" prefWidth="15.0" text="Id" visible="false"/>
                </columns>
            </TableView>
        </fx:define>
    </Pagination>

And in my controller I have added the same parts as in this example. And everything is working fine. Thanks a lot!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexey Shabramov
  • 730
  • 1
  • 16
  • 37

1 Answers1

4

Just define the Pagination in statistics.fxml:

<Pagination fx:id="pagination"/>

and then configure it in the controller for that class:

@FXML
private TableView<StatisticsRemarkTableDto> statisticsTableView;

@FXML
private Pagination pagination ;

public void initialize() {
    pagination.setPageFactory(this::createPage);
    // etc...
}

Since the table itself is placed in the scene graph by the page factory, it isn't part of the scene graph until after the initialize() method is invoked, i.e. after the FXML has been parsed. So you can either just define the table in the controller directly (and not include it in the FXML file at all):

private TableView<StatisticsRemarkTableDto> statisticsTableView;

@FXML
private Pagination pagination ;

public void initialize() {

    statisticsTableView = new TableView<>();
    // create columns, etc...
    pagination.setPageFactory(this::createPage);
    // etc...
}

or you can define it in FXML in a <fx:define> block:

<Pagination fx:id="pagination">
    <fx:define>
        <TableView fx:id="statisticsTableView">
            <columns>
                <TableColumn fx:id="objectColumn" ... />
                <!-- etc -->
            </columns>
        </TableView>
    </fx:define>
</Pagination>

and then inject it into the controller with @FXML in the usual way.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Finally i found the solutions to resolve my issue :) Thank you very much :) – Alexey Shabramov Apr 17 '16 at 13:16
  • How can you combine with a search filter ? Because i've added the Textfield (which worked before i've added pagination), then it's doing nothing, should i have to include this in pagination ? – sleakerz Feb 21 '18 at 14:56
  • @James_C In the last scenario, what does any related "pagination" code in the controller look like? – vic Nov 16 '22 at 23:18