Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

New Contributor
Posts: 2
Registered: ‎03-31-2012
My Device: 111
My Carrier: 222

About TouchEvent,how can get the clicked field?

In my screen have a custom field, but when I click anywhere it can trigger the custom filed's touchEvent method.

I don't know why?

In touchEvent I could not know which field have been clicked,some people write like this:

int x = message.getX(1);
int y = message.getY(1);

XYRect rect = filed.getExtent();
if (rect.contains(x, y)) {
 // xx field has been clicked.



but rect.contains(x, y) always false,I can't know witch field has been clicked.

Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: About TouchEvent,how can get the clicked field?

The problem with touchEvent, is if there is not a Field that is actually where the user touched, the touchevent is passed to the Field with focus.  This means that you can clicked even when the user has touched no where near your Field. 


To prevent this actually triggering an event on your field, most people will add code like the code included in your Post, to the touchEvent of every Field.  The code will ignore touchEvents that are outside the Field.


Some people actually want to get these events, even when the touch is not in their area.  For example you might be displaying pages and the user should be able to click anywhere to move the page forward.  So you will have a Forward button in focus, and the user can tap anywhere and the forward buttons touchEvent will get notified. 


Hope this clarifies the mystery.