• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JListのアイテムを範囲指定で選択
#navi(../)
*JListのアイテムを範囲指定で選択 [#va695101]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-08-14~
更新日:&lastmod;

#contents

**概要 [#x17f8eee]
JListのアイテムをラバーバンドで範囲指定して選択します。

#screenshot

**サンプルコード [#t7aa984b]
#code{{
 class MyList extends JList {
   private final Color rcolor;
   private final Color pcolor;
   private final AlphaComposite alcomp
   private final Polygon polygon = new Polygon();
   private final Line2D line = new Line2D.Double();
   private Point srcPoint = null;
   public MyList(ListModel model) {
     super(model);
     rcolor = SystemColor.activeCaption;
     pcolor = makeColor(rcolor);
     alcomp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
     setLayoutOrientation(JList.HORIZONTAL_WRAP);
     setVisibleRowCount(0);
     setFixedCellWidth(62);
     setFixedCellHeight(62);
     setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
     setCellRenderer(new ListCellRenderer() {
       public Component getListCellRendererComponent(
           JList list, Object value, int index, boolean isSelected,
           boolean cellHasFocus) {
         MyIcon icon = (MyIcon)getModel().getElementAt(index);
         icon.setSelected(isSelected);
         icon.setFocused(cellHasFocus);
         return icon;
       }
     });
     addMouseMotionListener(new MouseMotionAdapter() {
       public void mouseDragged(MouseEvent e){
         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);
         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();
       }
     });
     addMouseListener(new MouseAdapter() {
       public void mouseReleased(MouseEvent e) {
         srcPoint = null;
         repaint();
       }
       public void mousePressed(MouseEvent e) {
         if(locationToIndex(e.getPoint())<0) {
           clearSelection();
           repaint();
         }else{
           int index = locationToIndex(e.getPoint());
           MyIcon icon = (MyIcon)getModel().getElementAt(index);
           Rectangle rect = getCellBounds(index,index);
           if(!rect.contains(e.getPoint())) {
             clearSelection();
             repaint();
           }
         }
       }
     });
   }
   public void paintComponent(Graphics g) {
     super.paintComponent(g);
     if(srcPoint==null) return;
     Graphics2D g2d = (Graphics2D) g;
     g2d.setPaint(rcolor);
     g2d.drawPolygon(polygon);
     g2d.setComposite(alcomp);
     g2d.setPaint(pcolor);
     g2d.fillPolygon(polygon);
   }
   private int[] getIntersectsIcons(Shape p) {
     ListModel model = getModel();
     Vector list = new Vector(model.getSize());
     for(int i=0;i<model.getSize();i++) {
       Rectangle r = MyList.this.getCellBounds(i,i);
       if(p.intersects(r)) {
         list.add(new Integer(i));
       }
     }
     int[] il = new int[list.size()];
     for(int i=0;i<list.size();i++) {
       il[i] = ((Integer)list.get(i)).intValue();
     }
     return il;
   }
   private Color makeColor(Color c) {
     int r = c.getRed();
     int g = c.getGreen();
     int b = c.getBlue();
     if(r>g) return (r>b)?new Color(r,0,0):new Color(0,0,b);
     else    return (g>b)?new Color(0,g,0):new Color(0,0,b);
   }
 }
class MyList extends JList {
  private final Color rcolor;
  private final Color pcolor;
  private final AlphaComposite alcomp
  private final Polygon polygon = new Polygon();
  private final Line2D line = new Line2D.Double();
  private Point srcPoint = null;
  public MyList(ListModel model) {
    super(model);
    rcolor = SystemColor.activeCaption;
    pcolor = makeColor(rcolor);
    alcomp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
    setLayoutOrientation(JList.HORIZONTAL_WRAP);
    setVisibleRowCount(0);
    setFixedCellWidth(62);
    setFixedCellHeight(62);
    setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    setCellRenderer(new ListCellRenderer() {
      public Component getListCellRendererComponent(
          JList list, Object value, int index, boolean isSelected,
          boolean cellHasFocus) {
        MyIcon icon = (MyIcon)getModel().getElementAt(index);
        icon.setSelected(isSelected);
        icon.setFocused(cellHasFocus);
        return icon;
      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e){
        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);
        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();
      }
    });
    addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        srcPoint = null;
        repaint();
      }
      public void mousePressed(MouseEvent e) {
        if(locationToIndex(e.getPoint())<0) {
          clearSelection();
          repaint();
        }else{
          int index = locationToIndex(e.getPoint());
          MyIcon icon = (MyIcon)getModel().getElementAt(index);
          Rectangle rect = getCellBounds(index,index);
          if(!rect.contains(e.getPoint())) {
            clearSelection();
            repaint();
          }
        }
      }
    });
  }
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(srcPoint==null) return;
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(rcolor);
    g2d.drawPolygon(polygon);
    g2d.setComposite(alcomp);
    g2d.setPaint(pcolor);
    g2d.fillPolygon(polygon);
  }
  private int[] getIntersectsIcons(Shape p) {
    ListModel model = getModel();
    Vector list = new Vector(model.getSize());
    for(int i=0;i<model.getSize();i++) {
      Rectangle r = MyList.this.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;
  }
  private Color makeColor(Color c) {
    int r = c.getRed();
    int g = c.getGreen();
    int b = c.getBlue();
    if(r>g) return (r>b)?new Color(r,0,0):new Color(0,0,b);
    else    return (g>b)?new Color(0,g,0):new Color(0,0,b);
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#e178a26c]
JListにマウスリスナーを設定して、ドラッグに応じて矩形を描画しています。このとき、その矩形の内部にアイテムアイコンが重なる場合は、それを選択状態に変更しています。選択範囲が矩形にならずに直線になっている場合は、別途その直線と交差するアイテムを選択するようにしています。

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

**参考リンク [#o8eb5cf2]
-[[Can someone optimise the following code ?>http://forum.java.sun.com/thread.jspa?threadID=757107]]
-[[XP Style Icons - Windows Application Icon, Software XP Icons>http://www.icongalore.com/]]

**コメント [#re81cc47]
#comment