我的JavaFX TableView Pagination继续显示所有行

时间:2017-04-19 20:38:24

标签: java javafx

我的JavaFX表显示分页按钮编号正确,但它一直显示所有行,而我将行限制设置为100,它检测页数但是它会一直显示所有数据,而页面按钮因为所有行都无用显示。下面是我的代码。

enter image description here

我的数据实体模型(POJO)

@Entity
@Table(name="products")
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "product_id", unique = true, nullable = false)
private int ProductId;

@Column(name = "product_code", unique = true, nullable = false)
private String ProductCode;

@Column(name = "product_name", unique = true, nullable = false)
private String ProductName;



public Product(int productId, String productCode, String productName) 
{
    this.ProductId = productId;
    this.ProductCode = productCode;
    this.ProductName = productName;
}



public Product() {
    // TODO Auto-generated constructor stub
}



public int getProductId() {
    return ProductId;
}

public void setProductId(int productId) {
    this.ProductId = productId;
}

public String getProductCode() {
    return ProductCode;
}

public void setProductCode(String productCode) {
    this.ProductCode = productCode;
}

public String getProductName() {
    return ProductName;
}

public void setProductName(String productName) {
    this.ProductName = productName;
}

}

我的控制器

public class SampleController implements Initializable {

@FXML
private BorderPane borderpane;

@FXML
private StackPane stackpane;

@FXML
private TableView<Product> ProductTableView;
@FXML
private TableColumn<Product, Integer> clm1;
@FXML
private TableColumn<Product, String> clm2;
@FXML
private TableColumn<Product, String> clm3;

ProgressIndicator progressIndicator = new ProgressIndicator();

private final static int dataSize = 200;
private final static int rowsPerPage = 100;

private final ObservableList<Product> dataList = 
FXCollections.observableArrayList(new ArrayList<Product>
(rowsPerPage));

@Override
public void initialize(URL url, ResourceBundle rb) {
    ProductTableView.setItems(dataList);
    clm1.setCellValueFactory(new PropertyValueFactory<>("ProductId"));
    clm2.setCellValueFactory(new PropertyValueFactory<>
    ("ProductCode"));
    clm3.setCellValueFactory(new PropertyValueFactory<>
    ("ProductName"));

    Pagination pagination = new Pagination((dataSize / rowsPerPage + 
    1), 0);
    progressIndicator.setMaxSize(800, 200);
    //borderpane.getChildren().add(ProductTableView);
    //stackpane.getChildren().add(ProductTableView);
    stackpane.getChildren().add(progressIndicator);

    pagination.setPageFactory((final Integer pageIndex) -> {
        progressIndicator.setVisible(true);

        dataList.clear();

        // long running background task
        new Thread() {
            public void run() {
                try {
                    int fromIndex = pageIndex * rowsPerPage;
                    int toIndex = Math.min(fromIndex + rowsPerPage, 
                    dataSize);

                    List<Product> loadedList = loadData(fromIndex, 
                    toIndex);

                    Platform.runLater(() -> 
                    dataList.setAll(loadedList));

                } finally {

                    Platform.runLater(() -> 
                    progressIndicator.setVisible(false));

                }
            }
        }.start();

        //fixed it was saying "return borderpane"!!!
        return ProductTableView;
    });



     BorderPane borderpane = new BorderPane(pagination);

    stackpane.getChildren().add(borderpane);
    }

    private List<Product> loadData(int fromIndex, int toIndex) {
    Session session =  
    HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;
    tx = session.beginTransaction();
    Query query = session.createQuery("from Product");

    //List<Product> list = 
    query.list();//getAllProductsDatabase().list();
    // List<Product> list = new ArrayList<Product>();
    List<Product> list = query.list();
    // list = query.list();
    try {
    for(Product productlist:list){
     System.out.println(productlist.getProductId()+" 
    "+productlist.getProductCode()+" "+productlist.getProductName());

     }
    Thread.sleep(201);}
    catch( Exception e) {
        e.printStackTrace();
    }
    System.out.println("Size: "+ list.size());
    return list;
    }

    @FXML
    private void onTableClick(MouseEvent event) {
    System.out.println("Click");

    Product selectedId = 
    ProductTableView.getSelectionModel().getSelectedItem();
    System.out.println(selectedId.getProductId());
    System.out.println(selectedId.getProductCode());
    System.out.println(selectedId.getProductName());
}



}

我的FXML

<AnchorPane maxHeight="800.0" maxWidth="1000.0" prefHeight="800.0" 
prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.111" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="application.SampleController">
 <children>
  <BorderPane maxHeight="800.0" maxWidth="1000.0" prefHeight="800.0" 
    prefWidth="900.0">
     <top>
        <StackPane fx:id="stackpane" maxWidth="800.0" 
         prefHeight="1000.0" prefWidth="900.0" 
             BorderPane.alignment="CENTER">
           <children>
              <TableView fx:id="ProductTableView" 
               onMouseClicked="#onTableClick" maxHeight="800.0" 
               maxWidth="1000.0" prefHeight="800.0" 
               prefWidth="1000.0">
                <columns>
                  <TableColumn fx:id="clm1" prefWidth="102.0" 
                    text="Product ID" />
                    <TableColumn fx:id="clm2" prefWidth="123.0" 
                    text="Product Code" />
                    <TableColumn fx:id="clm3" prefWidth="164.0" 
                    text="Product Column" />
                    </columns>
                    </TableView>
               </children>
            </StackPane>
         </top>
      </BorderPane>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:1)

我认为您没有设置限制fromIndextoIndex,我认为此代码只会将loadData(int fromIndex, int toIndex)方法修改为此问题。

private List<Product> loadData(int fromIndex, int toIndex) {
    Session session =  
    HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;
    tx = session.beginTransaction();
    Query query = session.createQuery("from Product P WHERE P.id >= " + fromIndex + " AND P.id < " + toIndex);

    List<Product> list = query.list();
    try {
        for(Product productlist:list){
         System.out.println(productlist.getProductId()+" 
        "+productlist.getProductCode()+" "+productlist.getProductName());

         }
        Thread.sleep(201);
    }catch( Exception e) {
        e.printStackTrace();
    }
    System.out.println("Size: "+ list.size());
    return list;
    }

我做了什么,我改变了这一点。

Query query = session.createQuery("from Product");

对此。

Query query = session.createQuery("from Product P WHERE P.id >= " +
 fromIndex + " AND P.id < " + toIndex);

编辑1

要获得完全100行,即使您删除了一行,也必须使用此代码。

这是执行此操作的代码:

Query query = session.createQuery("from Product P")
.setFirstResult(fromIndex)
.setMaxResults(100);
  

第一种方法:

setFirstResult(fromIndex)

设置要检索的第一个结果的位置 fromIndex是第一个结果的位置,从0开始编号。

  

第二种方法:

setMaxResults(100)

设置要检索的最大结果数 100是要检索的最大结果数。