2016-04-16 34 views
1

私のプロジェクト(Spring、Hibernate e.t.c)でJavaFXを使用しています。JavaFX TableViewページネーション - fxmlベースのソリューションを作成できません

私はTableViewのページネーションを作成しようとしています。私は多くの合理的な解決策を見つけましたが、それらはすべてコントローラベースのソリューションです。彼らの最高のものはthis solutionです。

私の状況では、SceneBuilderで作成された複数のfxmlファイルを使用しています。これは、すべての巨大なデザインオプションとTabPaneを含んでいて、1つのタブには1つのFXMLファイルと1つのコントローラがあります。私のstatistics.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> 

私はテーブルビューとを作成しています:

私はそれをロードしています他人をインポートし、私のメインクラス内の1つの基本的なファイルを持っています列:

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

と確認例:

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

だけstatistics.fxmlPaginationを定義:次に

<Pagination fx:id="pagination"/> 

及びそのクラスのコントローラにそれを設定:

@FXML 
private TableView<StatisticsRemarkTableDto> statisticsTableView; 

@FXML 
private Pagination pagination ; 

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

テーブル自体はによってシーングラフ内に配置されているのでFXMLが解析された後で、initialize()メソッドが呼び出されるまで、シーングラフの一部ではありません。だから、どちらかだけのコントローラ内のテーブルを定義することができ、直接(と全くFXMLファイルに含めない):

private TableView<StatisticsRemarkTableDto> statisticsTableView; 

@FXML 
private Pagination pagination ; 

public void initialize() { 

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

か、<fx:define>ブロックにFXMLでそれを定義することができます。

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

と交換し、コントローラに通常の方法で@FXMLで注入します。

+0

最後に、私の問題を解決するための解決策を見つけました:)ありがとうございました:) –

+0

検索フィルターと組み合わせるにはどうすればいいですか? テキストフィールド(ページ番号を追加する前に機能していた)を追加したので、何もしません。これをページングに含める必要がありますか? – sleakerz

関連する問題