简体   繁体   English

JavaFX TableView分页-无法创建基于fxml的解决方案

[英]JavaFX TableView pagination - can not create fxml based solution

I am using JavaFX in my project (also Spring, Hibernate etc). 我在项目中使用JavaFX(还包括Spring,Hibernate等)。

I am trying to create Pagination for TableView. 我正在尝试为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. 但是在我的情况下-我正在使用在SceneBuilder中创建的具有多个巨大设计选项的多个fxml文件,其中包含TabPane,对于一个Tab,我有一个FXML文件和一个控制器。

I have one basic file which imports others, and in my Main class I am loading it: 我有一个导入其他文件的基本文件,并且在我的Main类中正在加载它:

在此处输入图片说明

<?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: 在我的statistics.fxml的控制器类中,我正在创建TableView和列:

@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: 我的Main方法如下所示:

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: 在所有TableView-pagination示例中,在Main类中都添加了分页,就像其他有效示例中的代码一样:

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? 有什么办法可以解决此问题并将分页添加到我的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 . 感谢James_D 及其解决方案 I have tried this variant and it worked fine. 我已经尝试过这种变体,并且效果很好。 My TableView is now with pagination. 我的TableView现在具有分页功能。

I have added to my statistical.fxml: 我已经添加到我的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! 非常感谢!

Just define the Pagination in statistics.fxml : 只需在statistics.fxml定义Pagination

<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, ie after the FXML has been parsed. 由于表本身是由页面工厂放置在场景图中的,因此直到调用initialize()方法之后,即在解析FXML之后,它才是场景图的一部分。 So you can either just define the table in the controller directly (and not include it in the FXML file at all): 因此,您可以直接在控制器中定义表(而根本不将其包括在FXML文件中):

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: 或者您可以在FXML中的<fx:define>块中定义它:

<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. 然后以常规方式使用@FXML将其注入到控制器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM