2016-04-16 155 views
1

我在我的项目中使用JavaFX(也是Spring,Hibernate e.t.c)。JavaFX TableView分页 - 无法创建基于fxml的解决方案

我正在尝试为TableView创建分页。我找到了许多合理的解决方案,但所有的解决方案都是基于控制器的。其中最好的在this solution

但在我的情况 - 我使用的是在SceneBuilder中创建的多个fxml文件,包含所有巨大的设计选项,其中包含TabPane和一个Tab,我有一个FXML文件和一个控制器。

我有进口别人,在我的主要类我加载它一个基本的文件:

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> 

在我的控制器类我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; 

和所有其他的东西,如this solution。 来自实体的信息被添加到我的表,一切工作正常。但!

问题在我尝试添加分页时开始,因为我无法理解应该如何执行此操作。我的主要方法是这样的:

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(); 
    } 
} 

在所有的TableView,分页例子在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); 
    } 

有一些方法通过这个问题,并将分页添加到我的TableView? 我会很高兴在这个问题上的任何答案,这个问题不会在第一天受到影响。

分辨!:

感谢James_Dhis solution。 我试过这个变种,它工作正常。我的TableView现在与分页。

我已经加入到我的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> 

在我控制我已经添加了相同的部分this example。一切工作正常。非常感谢!

回答

2

只要定义的Paginationstatistics.fxml

<Pagination fx:id="pagination"/> 

和然后将其配置在控制器该类:

@FXML 
private TableView<StatisticsRemarkTableDto> statisticsTableView; 

@FXML 
private Pagination pagination ; 

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

由于表本身被放置在所述场景图通过页面工厂,直到initialize()方法被调用后,即在FXML被解析之后,它才是场景图的一部分。所以,你可以只定义在控制器中的表直接(而不是在FXML文件包括它在所有):

private TableView<StatisticsRemarkTableDto> statisticsTableView; 

@FXML 
private Pagination pagination ; 

public void initialize() { 

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

,或者你可以在FXML在<fx:define>块定义它:

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

,然后以通常的方式将其注入控制器@FXML

+0

最后我找到了解决我的问题的解决方案:)非常感谢:) –

+0

如何结合搜索过滤器? 因为我已经添加了Textfield(在我添加分页之前工作),那么它什么都不做,我是否应该将它包含在分页中? – sleakerz