Skip to content

Instantly share code, notes, and snippets.

@khannedy
Created October 16, 2011 04:36
Show Gist options
  • Select an option

  • Save khannedy/1290513 to your computer and use it in GitHub Desktop.

Select an option

Save khannedy/1290513 to your computer and use it in GitHub Desktop.
Membuat Pagination di JTable Menggunakan JPagination
final ProductService productService = new ProductService();
final ProductTableModel tableModel = new ProductTableModel();
tableModel.setProducts(productService.load(0, 100));
jTableDemo.setModel(tableModel);
int count = Integer.valueOf(productService.count().toString());
final PaginationModel paginationModel = new DefaultPaginationModel(100, count);
jPaginationDemo.setModel(paginationModel);
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));
}
});
@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
}
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