[英]JavaFx @FXML Pagination injected to my controller class can't be modified
[英]JavaFX TableView pagination - can not create fxml based solution
我在項目中使用JavaFX(還包括Spring,Hibernate等)。
我正在嘗試為TableView創建分頁。 我找到了許多合理的解決方案,但所有這些都是基於控制器的解決方案。 他們最好的解決方案 。
但是在我的情況下-我正在使用在SceneBuilder中創建的具有多個巨大設計選項的多個fxml文件,其中包含TabPane,對於一個Tab,我有一個FXML文件和一個控制器。
我有一個導入其他文件的基本文件,並且在我的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>
在我的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;
以及其他所有類似的解決方案 。 來自該實體的信息已添加到我的表中,並且一切正常。 但!
當我嘗試添加分頁時,問題就開始了,因為我不知道該怎么做。 我的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();
}
}
在所有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);
}
有什么辦法可以解決此問題並將分頁添加到我的TableView? 我會很高興收到關於這個問題的任何答復,第一天就不會遇到問題。
解決!:
感謝James_D 及其解決方案 。 我已經嘗試過這種變體,並且效果很好。 我的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>
在我的控制器中,我添加了與本示例相同的部分。 而且一切正常。 非常感謝!
只需在statistics.fxml
定義Pagination
:
<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
將其注入到控制器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.