I have a JPanel with a bunch of JComponents inside. Now I want that the
user can drag the mouse over all of them and draw a selection rectangle
over them.
How do I do that? Do I have to use and implement the glassPane?
Isn't there a standard method? It's what every File Explorer does: There
are some Icons and you draw a selection rectangle to select some of
them... I can handle the selection later. Now I just want to draw the
selection rectangle.
Thanks!
Andi
first, add MouseListener and MouseMotionListener to your JPanel.
in MouseListener.mousePressed you must save pressed point (pressPoint)
in MouseMotionListener.mouseDragged you save your dragged point. (dragPoint)
you need also one variable to save last dragged point. (oldDragPoint) and
call repaint();
in JPanel.paintComponent you first call super.paintComponent then
you set g.setXORMode(Color.<youLike>)
g.drawRect(pressPoint.x, pressPoint.y, pressPoont.x - oldDragPoint.x,
pressPoint.y - oldDragPoint.y);
g.drawRect(pressPoint.x, pressPoint.y, pressPoont.x - dragPoint.x,
pressPoint.y - dragPoint.y);
oldDragPoint = dragPoint;
g.setpaintMode();
thats it!
PS. its somebit symplyfied. may be its better if you instead of drawing one
rect draw 4 lines.
I experienced that in xor mode it could be pretty slow if rectangle is big.
> I have a JPanel with a bunch of JComponents inside. Now I want that the
> user can drag the mouse over all of them and draw a selection rectangle
[quoted text clipped - 9 lines]
> Thanks!
> Andi
ak - 23 Nov 2003 09:55 GMT
sorry I made mistake,
you mast call repaint() in MouseMotionListener.mouseDragged after you saved
dragPoint
> first, add MouseListener and MouseMotionListener to your JPanel.
>
[quoted text clipped - 32 lines]
> > Thanks!
> > Andi