How to handle mouse events in JPanel?

Discussion in 'Java' started by Leo Smith, May 26, 2006.

  1. Leo Smith

    Leo Smith Guest

    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...r.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?
     
    Leo Smith, May 26, 2006
    #1
    1. Advertising

  2. Leo Smith

    Piet71 Guest

    > 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...r.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.

    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
     
    Piet71, May 26, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,029
  2. Replies:
    4
    Views:
    4,634
    Brendan Guild
    Oct 23, 2005
  3. Replies:
    8
    Views:
    906
    Andrew Thompson
    Sep 26, 2007
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,544
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,444
    t1m1976
    Nov 7, 2010
Loading...

Share This Page