TITLE:RowFilterでJTableのページ分割

RowFilterでJTableのページ分割

編集者:Terai Atsuhiro~

作成日:2007-11-05
更新日:2021-02-17 (水) 08:21:11
  • 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: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTUiUh8yiI/AAAAAAAAAmM/eY1zd24d0ac/s800/TablePagination.png hreflang:
       href: https://java-swing-tips.blogspot.com/2008/03/jtable-pagination-example-using.html
       lang: en

概要

JDK 6で導入されたRowFilterを使って、JTableの行をPagination風に分割して表示します。

概要

JDK 6 で導入されたRowFilterを使って、JTableの行をPagination風に分割して表示します。

サンプルコード

#spanend
#spanadd
private static int LR_PAGE_SIZE = 5;
#spanend

#spandel
#screenshot
#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

#spandel
**サンプルコード [#l576bdcf]
#spanend
#spandel
#code{{
#spanend
#spandel
void initLinkBox(Box box, TestModel m, final TableRowSorter<TestModel> sorter,
#spanend
                 final int size, final int step) {
  m.setRowCount(0);
  for(int i=0;i<size;i++) {
    m.addTest(new Test("Name"+i, (i%2==0)?"":"comment..."));
#spanadd
private void initLinkBox(final int itemsPerPage, final int currentPageIndex) {
#spanend
  // 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;
    }
  });
#spanadd

#spanend
  int startPageIndex = currentPageIndex - LR_PAGE_SIZE;
  if (startPageIndex <= 0) {
    startPageIndex = 1;
  }
#spanadd

#spanend
#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;
  }
#spanadd

#spanend
  box.removeAll();
  final ButtonGroup bg = new ButtonGroup();
  if (startPageIndex >= endPageIndex) {
    // if I only have one page, Y don't want to see pagination buttons
    // suggested by erServi
    return;
  }
#spanadd

#spanend
  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());
  box.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
  int page = 0;
  for(int i=0;i<size;i+=step) {
    page++;
    final int index = i;
    final JRadioButton rb = new JRadioButton(""+page);
    final RowFilter<TestModel,Integer> filter = new RowFilter<TestModel,Integer>() {
      @Override
      public boolean include(Entry<? extends TestModel, ? extends Integer> entry) {
        int ei = entry.getIdentifier();
        return (index<=ei && ei<index+step);
      }
    };
    rb.setForeground(Color.BLUE);
    rb.setUI(ui);
    rb.setAction(new AbstractAction(""+page) {
      @Override
      public void actionPerformed(ActionEvent e) {
        sorter.setRowFilter(filter);
      }
    });
    box.add(rb);
    bg.add(rb);
    if(i==0) {
      rb.setSelected(true);
      sorter.setRowFilter(filter);
    }
  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();
}
View in GitHub: Java, Kotlin
  • &jnlp;
  • &jar;
  • &zip;

解説

上記のサンプルは、検索サイトなどでよく使われている、PaginationをJTableで行っています。

解説

上記のサンプルは、検索サイトなどでよく使われているPagination風の処理をJTableで行っています。 ただし、ページ数が大量にある場合の処理や、前へ、次へなどの実装は無視して、ある位置から一定の行数だけ表示するフィルタを予め作成し、これを上部のJRadioButton*1で切り替えるだけになっています。
  • ある位置から一定の行数だけ表示するフィルタを予め作成し、これを上部のJRadioButtonで切り替え
    • このJRadioButtonBasicRadioButtonUIを継承して見た目だけリンク風に変更している
  • モデルのインデックス順でフィルタリングしているため、ソートを行っても表示される行の範囲内で変化する
また、モデルのインデックス順でフィルタリングしているため、ソートを行っても、表示される行の順番が変化するだけになります。

コメント

参考リンク

コメント