CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2011
    Posts
    8

    Mouse event question

    when press mouse on a component(such as a text area),How to get the component's name use mouse event?

    my code like this:

    ///////////////////////////////////////
    public void mousePressed(MouseEvent e)
    { Syesem.out.println(e.getSource());
    }
    /////////////////////////////////////////

    but "e.getSource()" show many information of the component but doesn't show the name of the component.

    What Should I do?

    thank you!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mouse event question

    e.getSource() returns the source of the event ie the object that generated the event. MouseEvent also has a getComponent() method which returns the Component that generated the event. You can use either method, they both return the same object, but the second method saves you having to cast the returned object to Component.

    Once you have the Component that generated the event, call it's getName() method.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2011
    Posts
    8

    Re: Mouse event question

    Thank you for the reply,but It seems that is is still impossible to get the name of a particular instance.
    by using e.getSource().
    I can only get the class name of the component:
    e.getSource().getClass().getName();
    but not the name of the instance.
    Any solution??
    Thank you!

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mouse event question

    That's not what I suggested you do. I suggested you do the following:
    Code:
    name = e.getComponent().getName();
    Of course you will have had to set a name for the component in the first place or you will get either an empty string or null.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured