Swing/TablePagination のバックアップの現在との差分(No.7)
TITLE:RowFilterでJTableのページ分割
Posted by terai at 2007-11-05
RowFilterでJTableのページ分割
JDK 6 で導入されたRowFilterを使って、JTableの行をPagination風に分割して表示します。-
category: swing
folder: TablePagination
title: RowFilterでJTableのページ分割
tags: [JTable, RowFilter, JRadioButton]
author: aterai
pubdate: 2007-11-05T14:35:32+09:00
description: JDK 6で導入されたRowFilterを使って、JTableの行をPagination風に分割して表示します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2008/03/jtable-pagination-example-using.html lang: en
概要
JDK 6
で導入されたRowFilter
を使って、JTable
の行をPagination
風に分割して表示します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
private static int LR_PAGE_SIZE = 5;
#spandel
private final TestModel model = new TestModel();
#spanend
#spandel
private final TableRowSorter<TestModel> sorter = new TableRowSorter<TestModel>(model);
#spanend
#spandel
private final Box box = Box.createHorizontalBox();
#spanend
#spanadd
private final String[] columnNames = {"Year", "String", "Comment"};
#spanend
#spanadd
private final DefaultTableModel model = new DefaultTableModel(null, columnNames) {
#spanend
@Override public Class<?> getColumnClass(int column) {
return (column == 0) ? Integer.class : Object.class;
}
#spanadd
};
#spanend
#spanadd
private TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
#spanend
#spanadd
private Box box = Box.createHorizontalBox();
#spanend
#spanadd
#spanend
private void initLinkBox(final int itemsPerPage, final int currentPageIndex) {
//assert currentPageIndex>0;
sorter.setRowFilter(makeRowFilter(itemsPerPage, currentPageIndex-1));
// assert currentPageIndex > 0;
sorter.setRowFilter(new RowFilter<TableModel, Integer>() {
@Override
public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
int ti = currentPageIndex - 1;
int ei = entry.getIdentifier();
return ti * itemsPerPage <= ei && ei < ti * itemsPerPage + itemsPerPage;
}
});
ArrayList<JRadioButton> l = new ArrayList<JRadioButton>();
int startPageIndex = currentPageIndex - LR_PAGE_SIZE;
if (startPageIndex <= 0) {
startPageIndex = 1;
}
int startPageIndex = currentPageIndex-LR_PAGE_SIZE;
if(startPageIndex<=0) startPageIndex = 1;
#spanadd
// #if 0 //BUG
#spanend
// int maxPageIndex = (model.getRowCount() / itemsPerPage) + 1;
#spanadd
// #else
#spanend
/* "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 v = rowCount % itemsPerPage == 0 ? 0 : 1;
int maxPageIndex = rowCount / itemsPerPage + v;
#spanadd
// #endif
#spanend
int endPageIndex = currentPageIndex + LR_PAGE_SIZE - 1;
if (endPageIndex > maxPageIndex) {
endPageIndex = maxPageIndex;
}
int maxPageIndex = (model.getRowCount()/itemsPerPage)+1;
int endPageIndex = currentPageIndex+LR_PAGE_SIZE-1;
if(endPageIndex>maxPageIndex) endPageIndex = maxPageIndex;
box.removeAll();
if (startPageIndex >= endPageIndex) {
// if I only have one page, Y don't want to see pagination buttons
// suggested by erServi
return;
}
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(currentPageIndex<maxPageIndex)
l.add(makePNRadioButton(itemsPerPage, currentPageIndex+1, "Next"));
#spandel
#spanend
box.removeAll();
ButtonGroup bg = new ButtonGroup();
JRadioButton f = makePrevNextRadioButton(
itemsPerPage, 1, "|<", currentPageIndex > 1);
box.add(f);
bg.add(f);
JRadioButton p = makePrevNextRadioButton(
itemsPerPage, currentPageIndex - 1, "<", currentPageIndex > 1);
box.add(p);
bg.add(p);
box.add(Box.createHorizontalGlue());
for(JRadioButton r:l) {
box.add(r); bg.add(r);
for (int i = startPageIndex; i <= endPageIndex; i++) {
JRadioButton c = makeRadioButton(itemsPerPage, currentPageIndex, i);
box.add(c);
bg.add(c);
}
box.add(Box.createHorizontalGlue());
JRadioButton n = makePrevNextRadioButton(
itemsPerPage, currentPageIndex + 1, ">", currentPageIndex < maxPageIndex);
box.add(n);
bg.add(n);
JRadioButton l = makePrevNextRadioButton(
itemsPerPage, maxPageIndex, ">|", currentPageIndex < maxPageIndex);
box.add(l);
bg.add(l);
box.revalidate();
box.repaint();
l.clear();
}
#spanend
#spandel
private JRadioButton makeRadioButton(
#spanend
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;
#spandel
}
#spanend
#spandel
private JRadioButton makePNRadioButton(
#spanend
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;
#spandel
}
#spanend
#spandel
private RowFilter<TestModel,Integer> makeRowFilter(
#spanend
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);
}
};
#spandel
}
#spanend
#spandel
解説
上記のサンプルは、検索サイトなどでよく使われている、PaginationをJTableで行っています。解説
上記のサンプルは、検索サイトなどでよく使われているPagination
風の処理をJTable
で行っています。
- ある位置から一定の行数だけ表示するフィルタを予め作成し、これを上部の
JRadioButton
で切り替え- この
JRadioButton
はBasicRadioButtonUI
を継承して見た目だけリンク風に変更している
- この
- モデルのインデックス順でフィルタリングしているため、ソートを行っても表示される行の範囲内で変化する
参考リンク
コメント
- Prev、Next ボタンなどを追加して、Google風のPaginationを行うように変更しました。 -- terai
- ブログで指摘されていた恥ずかしいバグ(paintメソッドでコンポーネントの状態を変更し、無限ループ、CPU100%)を修正 -- terai
コメント
-
Prev
、Next
ボタンなどを追加して、Google風のPagination
を行うように変更しました。 -- aterai - ブログで指摘されていた恥ずかしいバグ(
paint
メソッドでコンポーネントの状態を変更し、無限ループ、CPU100%
)を修正 -- aterai - blogspotで、無駄な空白ページができるバグを指摘してもらったので、こちらも修正しました。 -- aterai
- 先頭と最後にジャンプするボタンを追加。 -- aterai