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