• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableの行を移動
#navi(../)
*JTableの行を移動 [#u1a0d5a8]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-02-23~
更新日:&lastmod;

#contents

**概要 [#l9990a21]
ツールバーや、ポップアップメニューを使って、JTableの行を上下に移動します。

#screenshot

**サンプルコード [#yc463e85]
#code{{
class DownAction extends AbstractAction{
  public DownAction(String str) {
    super(str);
  }
  public void actionPerformed(ActionEvent evt) {
    downActionPerformed(evt);
  }
}
private void downActionPerformed(ActionEvent e) {
  System.out.println("-------- 下へ --------");
  int[] pos = table.getSelectedRows();
  if(pos==null || pos.length<=0) return;
  DefaultTableModel mdl = (DefaultTableModel) table.getModel();
  if((e.getModifiers() & ActionEvent.SHIFT_MASK)!=0) {
    mdl.moveRow(pos[0], pos[pos.length-1], mdl.getRowCount()-pos.length);
    table.setRowSelectionInterval(mdl.getRowCount()-pos.length,
      mdl.getRowCount()-1);
  }else{
    if(pos[pos.length-1]==mdl.getRowCount()-1) return;
    mdl.moveRow(pos[0], pos[pos.length-1], pos[0]+1);
    table.setRowSelectionInterval(pos[0]+1, pos[pos.length-1]+1);
  }
  scrollSelectedRow();
}

public void showRowPop(MouseEvent e) {
  int row     = table.rowAtPoint(e.getPoint());
  int count   = table.getSelectedRowCount();
  int[] ilist = table.getSelectedRows();
  boolean flg = true;
  for(int i=0;i<ilist.length;i++) {
    if(ilist[i]==row) {
      flg = false;
      break;
    }
  }
  if(row>0 && flg) table.setRowSelectionInterval(row, row);
  JPopupMenu pop = new JPopupMenu();
  Action act = new TestCreateAction("追加", null);
  act.setEnabled(count==1);
  pop.add(act);
  pop.addSeparator();
  act = new DeleteAction("削除", null);
  act.setEnabled(row>=0);
  pop.add(act);
  pop.addSeparator();
  act = new UpAction("上へ");
  act.setEnabled(count>0);
  pop.add(act);
  act = new DownAction("下へ");
  act.setEnabled(count>0);
  pop.add(act);
  pop.show((JComponent)e.getSource(), e.getX(), e.getY());
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#q2ca8783]
上記のサンプルでは、DefaultTableModel#moveRowメソッドを使用して、選択した行を上下に動かしています。シフトキーを押しながら、ツールバーの移動ボタンを押すとそれぞれ、先頭、末尾に移動します。

//**参考リンク
**コメント [#b4d206da]
- fun search test!<a href="http://fun-things.info/christmas-cards/corporate-business-christmas-cards.html">corporate business christmas cards</a>http://fun-things.info/christmas-gifts/bakery-basket-christmas-gift.htmlhttp://fun-things.info/christmas-gifts/basket-christmas-food-gift-gourmet.htmlhttp://fun-things.info/christmas-cards/christmas-rose-poem-card.html<a href="http://fun-things.info/christmas-gifts/buy-christmas-gifts-now,-pay-later..html">buy christmas gifts now, pay later.</a><a href="http://fun-things.info/christmas-cards/owl-christmas-card.html">owl christmas card</a>http://fun-things.info/christmas-gifts/2007-big-christmas-gifts.htmlhttp://fun-things.info/christmas-cards/free-christmas-card-print-outs.htmlhttp://fun-things.info/christmas-gifts/pictures-for-christmas-gifts.htmlhttp://fun-things.info/christmas-gifts/food-gift-recipes-christmas.htmlhttp://fun-things.info/christmas-gifts/luxury-christmas-gifts.html<a href="http://fun-things.info/christmas-gifts/creative-handmade-christmas-gift-ideas.html">creative handmade christmas gift ideas</a>http://fun-things.info/christmas-gifts/a-unique-christmas-gift-the-online-time-capsule-prweb.htmlhttp://fun-things.info/christmas-cards/antique-christian-christmas-cards.html<a href="http://fun-things.info/christmas-cards/old-time-christmas-cards.html">old time christmas cards</a> -- [[yellowbo]] &new{2007-12-12 (水) 14:06:24};

#comment