Created
October 16, 2011 04:36
-
-
Save khannedy/1290513 to your computer and use it in GitHub Desktop.
Membuat Pagination di JTable Menggunakan JPagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final ProductService productService = new ProductService(); | |
| final ProductTableModel tableModel = new ProductTableModel(); | |
| tableModel.setProducts(productService.load(0, 100)); | |
| jTableDemo.setModel(tableModel); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int count = Integer.valueOf(productService.count().toString()); | |
| final PaginationModel paginationModel = new DefaultPaginationModel(100, count); | |
| jPaginationDemo.setModel(paginationModel); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jPaginationDemo.addPaginationListener(new PaginationListener() { | |
| public void onPageChange(PaginationEvent event) { | |
| int first = (event.getCurrentPage() - 1) * event.getPageSize(); | |
| int max = event.getPageSize(); | |
| tableModel.setProducts(productService.load(first, max)); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Entity | |
| @Table(name = "products") | |
| public class Product implements Serializable { | |
| private static final long serialVersionUID = 1L; | |
| @Id | |
| @GeneratedValue | |
| @Column(name = "id") | |
| private Long id; | |
| @Column(name = "name") | |
| private String name; | |
| @Column(name = "price") | |
| private Long price; | |
| // getter and setter | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ProductTableModel extends AbstractTableModel { | |
| private static final long serialVersionUID = 1L; | |
| private List<Product> products; | |
| public ProductTableModel() { | |
| products = new ArrayList<Product>(0); | |
| } | |
| public void setProducts(List<Product> products) { | |
| this.products = products; | |
| fireTableDataChanged(); | |
| } | |
| public int getRowCount() { | |
| return products.size(); | |
| } | |
| public int getColumnCount() { | |
| return 3; | |
| } | |
| public Object getValueAt(int rowIndex, int columnIndex) { | |
| switch (columnIndex) { | |
| case 0: | |
| return products.get(rowIndex).getId(); | |
| case 1: | |
| return products.get(rowIndex).getName(); | |
| case 2: | |
| return products.get(rowIndex).getPrice(); | |
| default: | |
| return null; | |
| } | |
| } | |
| @Override | |
| public String getColumnName(int column) { | |
| switch (column) { | |
| case 0: | |
| return "Id"; | |
| case 1: | |
| return "Name"; | |
| case 2: | |
| return "Price"; | |
| default: | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment