-
September 21st, 2011, 04:06 AM
#1
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!
-
September 21st, 2011, 05:08 AM
#2
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.
-
September 21st, 2011, 04:25 PM
#3
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!
-
September 21st, 2011, 04:30 PM
#4
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|