Code
public class MainPanel extends JPanel {
private static final Color evenColor = new Color(240, 255, 250);
private static final LinkViewRadioButtonUI ui = new LinkViewRadioButtonUI();
private static int LR_PAGE_SIZE = 5;
private final TestModel model = new TestModel();
private final TableRowSorter< TestModel > sorter = new TableRowSorter< TestModel >(model);
private final Box box = Box.createHorizontalBox();
public MainPanel() {
super(new BorderLayout());
JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
Component c = super.prepareRenderer(tcr, row, column);
if(isRowSelected(row)) {
c.setForeground(getSelectionForeground());
c.setBackground(getSelectionBackground());
}else{
c.setForeground(getForeground());
c.setBackground((row%2==0)?evenColor:getBackground());
}
return c;
}
};
table.setFillsViewportHeight(true);
table.setIntercellSpacing(new Dimension());
table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setRowSorter(sorter);
for(int i=0;i <= 2008;i++) {
model.addTest(new Test(" Test: "+i, (i%2==0)?"":"comment..."));
}
initLinkBox(100, 1);
add(box, BorderLayout.NORTH);
add(new JScrollPane(table));
setPreferredSize(new Dimension(320, 240));
}
private void initLinkBox(final int itemsPerPage, final int currentPageIndex) {
//assert currentPageIndex > 0;
sorter.setRowFilter(makeRowFilter(itemsPerPage, currentPageIndex-1));
ArrayList< JRadioButton > l = new ArrayList< JRadioButton >();
int startPageIndex = currentPageIndex-LR_PAGE_SIZE;
if(startPageIndex <= 0) startPageIndex = 1;
//#if 0
//int maxPageIndex = (model.getRowCount()/itemsPerPage)+1;
//#else
/* "maxPageIndex" gives one blank page if the module of the division is not zero.
* pointed out by erServi
* e.g. rowCount=100, maxPageIndex=100
*/
int rowCount = model.getRowCount();
int maxPageIndex = (rowCount/itemsPerPage) + (rowCount%itemsPerPage==0?0:1);
//#endif
int endPageIndex = currentPageIndex+LR_PAGE_SIZE-1;
if(endPageIndex>maxPageIndex) endPageIndex = maxPageIndex;
if(currentPageIndex > 1)
l.add(makePNRadioButton(itemsPerPage, currentPageIndex-1, "Prev"));
//for(int i=startPageIndex;i <= endPageIndex;i++)
// l.add(makeRadioButton(itemsPerPage, currentPageIndex, i-1));
//if I only have one page, Y don't want to see pagination buttons
//suggested by erServi
if(startPageIndex < endPageIndex) {
for(int i=startPageIndex; i<= endPageIndex;i++) {
l.add(makeRadioButton(itemsPerPage, currentPageIndex, i-1));
}
}
if(currentPageIndex < maxPageIndex)
l.add(makePNRadioButton(itemsPerPage, currentPageIndex+1, "Next"));
box.removeAll();
ButtonGroup bg = new ButtonGroup();
box.add(Box.createHorizontalGlue());
for(JRadioButton r:l) {
box.add(r); bg.add(r);
}
box.add(Box.createHorizontalGlue());
box.revalidate();
box.repaint();
l.clear();
}
private JRadioButton makeRadioButton(final int itemsPerPage,
final int current, final int target) {
JRadioButton radio = new JRadioButton(""+(target+1));
radio.setForeground(Color.BLUE);
radio.setUI(ui);
if(target+1==current) {
radio.setSelected(true);
radio.setForeground(Color.BLACK);
}
radio.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
initLinkBox(itemsPerPage, target+1);
}
});
return radio;
}
private JRadioButton makePNRadioButton(final int itemsPerPage, final int target, String title) {
JRadioButton radio = new JRadioButton(title);
radio.setForeground(Color.BLUE);
radio.setUI(ui);
radio.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
initLinkBox(itemsPerPage, target);
}
});
return radio;
}
private RowFilter< TestModel, Integer > makeRowFilter(final int itemsPerPage, final int target) {
return new RowFilter< TestModel, Integer >() {
@Override
public boolean include(Entry< ? extends TestModel, ? extends Integer > entry) {
int ei = entry.getIdentifier();
return (target*itemsPerPage <= ei && ei < target*itemsPerPage+itemsPerPage);
}
};
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try{
UIManager.getInstalledLookAndFeels();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
e.printStackTrace();
}
final JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
References