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 / General / May 2006

Tip: Looking for answers? Try searching our database.

How to handle mouse events in JPanel?

Thread view: 
Leo Smith - 26 May 2006 21:46 GMT
Dear All,

I want to write a subclass of JPanel.

The new kind of JPanel can handle Mouse Events so that I can click
inside of it and create rectangles. I have looked at
MouseInputAdapter(abstract class)
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/event/MouseInputAdapter.html

and MouseInputHandler
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/basic/BasicDesktopIconU
I.MouseInputHandler.html#mousePressed(java.awt.event.MouseEvent
)

I still don't know how to do it. I need the information where the mouse
was pressed and where the mouse was released. But I don't know how to
use the methods in above classes to obtain such information.

Thank you very much.

BTW, how can I find out if such a new JPanel has been created by
somebody else and is already available?
Piet71 - 26 May 2006 22:23 GMT
> I want to write a subclass of JPanel.
>
[quoted text clipped - 9 lines]
> was pressed and where the mouse was released. But I don't know how to
> use the methods in above classes to obtain such information.
Add a MouseListener (for example, a class derived from
MouseInputAdapter or MouseAdapter) to your JPanel and override the
mouseXXX methods that you need. To get the position where the Mouse was
clicked, have a look at the getPoint() method (and of course at the
other methods as well!) of the MouseEvent class.
Noe that this is all possible without subclassing JPanel. For what you
seem to have in mind (some kind of drawing application) I would
recommend to use JComponent as a base class to avoid interference of
your drawing with the FlowLayout which comes with JPanel.
HTH, Piet


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



©2008 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.