import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
public MainClass() {
super("Paged JTable Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
PagingModel pm = new PagingModel();
pm.setPageSize(20);
JTable jt = new JTable(pm);
JScrollPane jsp = PagingModel.createPagingScrollPaneForTable(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass pt = new MainClass();
pt.setVisible(true);
}
}
class Record {
static String[] headers = { "Record Number", "Batch Number", "Reserved" };
static int counter;
String[] data;
public Record() {
data = new String[] { "" + (counter++), "" + System.currentTimeMillis(),
"Reserved" };
}
public String getValueAt(int i) { return data[i]; }
public static String getColumnName(int i) { return headers[i]; }
public static int getColumnCount() { return headers.length; }
}
class PagingModel extends AbstractTableModel {
protected int pageSize;
protected int pageOffset;
protected Record[] data;
public PagingModel() {
this(10000, 100);
}
public PagingModel(int numRows, int size) {
data = new Record[numRows];
pageSize = size;
for (int i = 0; i < data.length; i++) {
data[i] = new Record();
}
}
public int getRowCount() {
return Math.min(pageSize, data.length);
}
public int getColumnCount() {
return Record.getColumnCount();
}
public Object getValueAt(int row, int col) {
int realRow = row + (pageOffset * pageSize);
return data[realRow].getValueAt(col);
}
public String getColumnName(int col) {
return Record.getColumnName(col);
}
public int getPageOffset() {
return pageOffset;
}
public int getPageCount() {
return (int) Math.ceil((double) data.length / pageSize);
}
public int getRealRowCount() {
return data.length;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int s) {
if (s == pageSize) {
return;
}
int oldPageSize = pageSize;
pageSize = s;
pageOffset = (oldPageSize * pageOffset) / pageSize;
fireTableDataChanged();
}
public void pageDown() {
if (pageOffset < getPageCount() - 1) {
pageOffset++;
fireTableDataChanged();
}
}
public void pageUp() {
if (pageOffset > 0) {
pageOffset--;
fireTableDataChanged();
}
}
public static JScrollPane createPagingScrollPaneForTable(JTable jt) {
JScrollPane jsp = new JScrollPane(jt);
TableModel tmodel = jt.getModel();
if (!(tmodel instanceof PagingModel)) {
return jsp;
}
final PagingModel model = (PagingModel) tmodel;
final JButton upButton = new JButton("UP");
upButton.setEnabled(false);
final JButton downButton = new JButton("DOWN");
if (model.getPageCount() <= 1) {
downButton.setEnabled(false);
}
upButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
model.pageUp();
if (model.getPageOffset() == 0) {
upButton.setEnabled(false);
}
downButton.setEnabled(true);
}
});
downButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
model.pageDown();
if (model.getPageOffset() == (model.getPageCount() - 1)) {
downButton.setEnabled(false);
}
upButton.setEnabled(true);
}
});
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, upButton);
jsp.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, downButton);
return jsp;
}
}
|