2015-06-16 36 views
0

我正在做一個數據顯示應用程序,我需要添加一個選項到我的分頁,可以用來返回到第一頁(索引)或轉到最後一個。JavaFX分頁,加入<< and > >>選項

我已經嘗試添加按鈕到我的用戶界面,但它沒有工作,因爲我無法獲得最後的索引。

@FXML 
void goToLastIndex(ActionEvent event) { 
    int lastIndex = pagination.getPageCount(); 
    pagination.setCurrentPageIndex(lastIndex); 
} 
+0

你是什麼意思的「我無法獲得最後的指數」?換句話說,你爲'lastIndex'獲得什麼價值?由於'currentPageIndex'是從零開始的,你可能需要'pagination.setCurrentPageIndex(lastIndex-1)' –

回答

1

你看過了分頁的pageCountProperty。

/** 
* Returns the number of pages. 
*/ 
public final int getPageCount() { return pageCount.get(); } 

/** 
* The number of pages for this pagination control. This 
* value must be greater than or equal to 1. {@link #INDETERMINATE} 
* should be used as the page count if the total number of pages is unknown. 
* 
* The default is an {@link #INDETERMINATE} number of pages. 
*/ 
public final IntegerProperty pageCountProperty() { return pageCount; } 
+0

我仍然得到一個太大的索引。它返回2147483647,最後一個索引應該是961. – mangaalex95

+0

2147483647是Integer.MAX_VALUE 0x7fffffff的大小。你是否正確初始化你的控制? –

1

按照JavadocspageCount的默認值是Pagination.INDETERMINATE,其是(或​​多或少任意)等於Integer.MAX_VALUE。如果您的分頁數量固定(如果沒有,最後一頁沒有意義),那麼您應該通過調用constructor taking a page count value來初始化它,或者致電setPageCount(...)並指定頁數。

0

謝謝,我所需要做的就是創建一個變量來保存頁面的數量並將其與setOnAction結合使用。

int numberOfPage = (nbOfDataForCurrentType/ROW_PER_PAGE + 1); 
     pagination = new Pagination(numberOfPage, 0); 
     pagination.setPageFactory(param -> populateTableView(param)); 
     getChildren().add(pagination); 

     if (numberOfPage > 1) { 
      btnEnd.onActionProperty().set(event -> pagination.setCurrentPageIndex(numberOfPage)); 
      btnBegin.setOnAction(event -> pagination.setCurrentPageIndex(0)); 

      getChildren().add(btnBegin); 
      getChildren().add(btnEnd); 
     } 

    }); 
相關問題