Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

User draws selection rectangle over components?

Thread view: 
Andreas Schmidt - 22 Nov 2003 13:33 GMT
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
ak - 23 Nov 2003 09:52 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.