• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JListのアイテムを範囲指定で選択
#navi(../)
RIGHT:Posted by [[terai]] at 2006-08-14
RIGHT:Posted by [[aterai]] at 2006-08-14
*JListのアイテムを範囲指定で選択 [#va695101]
JListのアイテムをラバーバンドで範囲指定して選択します。

-&jnlp;
-&jar;
-&zip;

#screenshot
//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTSd-lu2aI/AAAAAAAAAi0/AQTsBqR1OUc/s800/RubberBanding.png)

**サンプルコード [#t7aa984b]
#code{{
class RubberBandingListener extends MouseInputAdapter {
  @Override
  public void mouseDragged(MouseEvent e) {
    setFocusable(true);
    if(srcPoint==null) srcPoint = e.getPoint();
    Point destPoint = e.getPoint();
    polygon.reset();
    polygon.addPoint(srcPoint.x,  srcPoint.y);
    polygon.addPoint(destPoint.x, srcPoint.y);
    polygon.addPoint(destPoint.x, destPoint.y);
    polygon.addPoint(srcPoint.x,  destPoint.y);
    //setSelectedIndices(getIntersectsIcons(polygon));
    if(srcPoint.getX()==destPoint.getX() || srcPoint.getY()==destPoint.getY()) {
      line.setLine(srcPoint.getX(),srcPoint.getY(),destPoint.getX(),destPoint.getY());
      setSelectedIndices(getIntersectsIcons(line));
    }else{
      setSelectedIndices(getIntersectsIcons(polygon));
    }
    repaint();
  }
  @Override
  public void mouseReleased(MouseEvent e) {
    setFocusable(true);
    srcPoint = null;
    repaint();
  }
  @Override
  public void mousePressed(MouseEvent e) {
    int index = locationToIndex(e.getPoint());
    Rectangle rect = getCellBounds(index,index);
    if(!rect.contains(e.getPoint())) {
      getSelectionModel().setLeadSelectionIndex(getModel().getSize());
      clearSelection();
      setFocusable(false);
    }else{
      setFocusable(true);
    }
  }
  private int[] getIntersectsIcons(Shape p) {
    ListModel model = getModel();
    Vector<Integer> list = new Vector<Integer>(model.getSize());
    for(int i=0;i<model.getSize();i++) {
      Rectangle r = getCellBounds(i,i);
      if(p.intersects(r)) {
        list.add(i);
      }
    }
    int[] il = new int[list.size()];
    for(int i=0;i<list.size();i++) {
      il[i] = list.get(i);
    }
    return il;
  }
}
}}

**解説 [#e178a26c]
上記のサンプルでは、JListにマウスリスナーを設定して、ドラッグに応じた矩形が描画されるようになっています。

この矩形の内部にアイテムアイコンが重なる場合は、それを選択状態に変更しています。選択範囲が矩形にならずに直線になっている場合は、別途その直線と交差するアイテムを選択するようにしています。

JList内のアイテムの配置は、JList#setLayoutOrientation(JList.HORIZONTAL_WRAP)メソッドを使っているため、水平方向に整列されます。

**参考リンク [#o8eb5cf2]
-[[Swing - Can someone optimise the following code ?>http://forums.sun.com/thread.jspa?threadID=757107]]
-[[XP Style Icons - Windows Application Icon, Software XP Icons>http://www.icongalore.com/]]
-[http://forums.sun.com/thread.jspa?threadID=757107 Swing - Can someone optimise the following code ?]
-[http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons]
-[[JListのアイテムをラバーバンドで複数選択、ドラック&ドロップで並べ替え>Swing/DragSelectDropReordering]]

**コメント [#re81cc47]
- 点線のアニメーション: [[プログラマメモ2: java ラバーバンドを表現するためのした調べ>http://programamemo2.blogspot.com/2007/08/java.html]] -- [[terai]] &new{2008-08-01 (金) 16:24:28};
- スクリーンショットなどを更新 -- [[terai]] &new{2008-10-06 (月) 21:29:19};
- 点線のアニメーション: [http://programamemo2.blogspot.com/2007/08/java.html プログラマメモ2: java ラバーバンドを表現するためのした調べ] -- [[aterai]] &new{2008-08-01 (金) 16:24:28};
- スクリーンショットなどを更新 -- [[aterai]] &new{2008-10-06 (月) 21:29:19};

#comment