Advertisement
zinch

Pagination Animation Example

Feb 20th, 2014
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. import com.sun.javafx.scene.traversal.TraversalEngine;
  2. import com.sun.javafx.scene.traversal.TraverseListener;
  3. import javafx.animation.KeyFrame;
  4. import javafx.animation.KeyValue;
  5. import javafx.animation.Timeline;
  6. import javafx.application.Application;
  7. import javafx.beans.property.SimpleStringProperty;
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. import javafx.geometry.Bounds;
  11. import javafx.scene.Node;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Pagination;
  14. import javafx.scene.control.TableColumn;
  15. import javafx.scene.control.TableView;
  16. import javafx.scene.control.cell.PropertyValueFactory;
  17. import javafx.scene.layout.AnchorPane;
  18. import javafx.scene.layout.VBox;
  19. import javafx.stage.Stage;
  20. import javafx.util.Callback;
  21. import javafx.util.Duration;
  22.  
  23. import static java.lang.Math.random;
  24.  
  25. public class MainApp1 extends Application
  26. {
  27.  
  28.     final ObservableList<Person> data = FXCollections.observableArrayList(
  29.             new Person("1", "Joe", "Pesci"),
  30.             new Person("32", "Rhonda", " Fleming's"),
  31.             new Person("32", "Humphrey", "Bogart"));
  32.     private Pagination pagination;
  33.  
  34.     public static void main(String[] args) throws Exception
  35.     {
  36.         launch(args);
  37.     }
  38.  
  39.     public int itemsPerPage()
  40.     {
  41.         return 1;
  42.     }
  43.  
  44.     public int rowsPerPage()
  45.     {
  46.         return 2;
  47.     }
  48.  
  49.     public VBox createPage(int pageIndex)
  50.     {
  51.         int lastIndex = 0;
  52.         int displace = data.size() % rowsPerPage();
  53.         if (displace > 0)
  54.         {
  55.             lastIndex = data.size() / rowsPerPage();
  56.         }
  57.         else
  58.         {
  59.             lastIndex = data.size() / rowsPerPage() - 1;
  60.  
  61.         }
  62.  
  63.         VBox box = new VBox(5);
  64.         int page = pageIndex * itemsPerPage();
  65.  
  66.         for (int i = page; i < page + itemsPerPage(); i++)
  67.         {
  68.             TableView<Person> table = new TableView<>();
  69.             TableColumn numCol = new TableColumn("ID");
  70.             numCol.setCellValueFactory(
  71.                     new PropertyValueFactory<Person, String>("num"));
  72.  
  73.             numCol.setMinWidth(20);
  74.  
  75.             TableColumn firstNameCol = new TableColumn("First Name");
  76.             firstNameCol.setCellValueFactory(
  77.                     new PropertyValueFactory<Person, String>("firstName"));
  78.  
  79.             firstNameCol.setMinWidth(160);
  80.  
  81.             TableColumn lastNameCol = new TableColumn("Last Name");
  82.             lastNameCol.setCellValueFactory(
  83.                     new PropertyValueFactory<Person, String>("lastName"));
  84.  
  85.             lastNameCol.setMinWidth(160);
  86.  
  87.             table.getColumns().addAll(numCol, firstNameCol, lastNameCol);
  88.             if (lastIndex == pageIndex)
  89.             {
  90.                 table.setItems(FXCollections.observableArrayList(data.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + displace)));
  91.             }
  92.             else
  93.             {
  94.                 table.setItems(FXCollections.observableArrayList(data.subList(pageIndex * rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
  95.             }
  96.  
  97.             box.getChildren().add(table);
  98.         }
  99.  
  100.         Timeline timeline = new Timeline();
  101.         timeline.getKeyFrames().add(
  102.                 new KeyFrame(Duration.ZERO,
  103.                         new KeyValue(box.opacityProperty(), 0)
  104.                 )
  105.         );
  106.  
  107.         for (int i = 1; i < 10; i++) {
  108.             timeline.getKeyFrames().add(
  109.                     new KeyFrame(new Duration(i * 500),
  110.                             new KeyValue(box.opacityProperty(), i / 10.0)
  111.                     )
  112.             );
  113.         }
  114.  
  115.         timeline.play();
  116.  
  117.         return box;
  118.     }
  119.  
  120.     @Override
  121.     public void start(final Stage stage) throws Exception
  122.     {
  123.  
  124.         pagination = new Pagination((data.size() / rowsPerPage() + 1), 0);
  125.         //   pagination = new Pagination(20 , 0);
  126.         //pagination.setStyle("-fx-border-color:red;");
  127.         pagination.setPageFactory(new Callback<Integer, Node>()
  128.         {
  129.             @Override
  130.             public Node call(Integer pageIndex)
  131.             {
  132.                 if (pageIndex > data.size() / rowsPerPage() + 1)
  133.                 {
  134.                     return null;
  135.                 }
  136.                 else
  137.                 {
  138.                     return createPage(pageIndex);
  139.                 }
  140.             }
  141.         });
  142.  
  143.         AnchorPane anchor = new AnchorPane();
  144.         AnchorPane.setTopAnchor(pagination, 10.0);
  145.         AnchorPane.setRightAnchor(pagination, 10.0);
  146.         AnchorPane.setBottomAnchor(pagination, 10.0);
  147.         AnchorPane.setLeftAnchor(pagination, 10.0);
  148.         anchor.getChildren().addAll(pagination);
  149.         Scene scene = new Scene(anchor, 400, 250);
  150.         stage.setScene(scene);
  151.         stage.setTitle("Table pager");
  152.         stage.show();
  153.     }
  154.  
  155.     public static class Person
  156.     {
  157.  
  158.         private final SimpleStringProperty num;
  159.         private final SimpleStringProperty firstName;
  160.         private final SimpleStringProperty lastName;
  161.  
  162.         private Person(String id, String fName, String lName)
  163.         {
  164.             this.firstName = new SimpleStringProperty(fName);
  165.             this.lastName = new SimpleStringProperty(lName);
  166.             this.num = new SimpleStringProperty(id);
  167.         }
  168.  
  169.         public String getFirstName()
  170.         {
  171.             return firstName.get();
  172.         }
  173.  
  174.         public void setFirstName(String fName)
  175.         {
  176.             firstName.set(fName);
  177.         }
  178.  
  179.         public String getLastName()
  180.         {
  181.             return lastName.get();
  182.         }
  183.  
  184.         public void setLastName(String fName)
  185.         {
  186.             lastName.set(fName);
  187.         }
  188.  
  189.         public String getNum()
  190.         {
  191.             return num.get();
  192.         }
  193.  
  194.         public void setNum(String id)
  195.         {
  196.             num.set(id);
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement