Welcome!

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

Reply
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem with CustomLabelField

 

Zany, With your code, I dont get any cmpile time error, but see exception while running.

 

My catch code in touchEvent()

catch(Exception e) {
                System.out.println("Excep - " + e.getMessage());
                e.printStackTrace();
            }

 

Gives this output.

Excep - null
No stack trace
Excep - null
No stack trace
Excep - null
No stack trace

 

when I right click my custom label (NOTE IT - only one right click & 3 times null exception). On right clicking, the label has the focus, but its not executed what it should do i.e. tje fieldChange event. Then when I left click on any place that is blank on the screen, the action of that label is executed.

 

Can you tell me what is this and how do I execute the code of action when the user right clicks on the label. I guess that's how it should be, right or am getting somewher wrong? If their is any action added in fieldChaged for that label, then only one right click should get the action executed,

 

Thanks

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Problem with CustomLabelField

code what i have given is only a snippet to handle the touch events. ok, the field what you are tring to set focus using (setFocus()) is a focusable component?

 

also instead of writing code as

System.out.println("Excep - " + e.getMessage());

try to change it as

System.out.println("Excep - " + e.toString());

 

it will give you what exception it was

with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem with CustomLabelField

 

Zany, I have used only your code for touchEvent, no other code is added for touch events.

 

I have a custom lable that is focusable. when I right clikc on that label, I find focus on it which is proper.

 

Now what I am asking for is, as when we click our button, fieldChange event is trapped and whatever we have written in fieldChanged for that button executes - Right. I have written code for my custom labels in fieldChange which executed when I left click on the label. If I want to execute same code when a user right clicks (i,e. for touch ) the lable, what should I do and how.

 

Here is the code of my custom label - :

    class ArrowLabel extends LabelField {
        private Font lblFont;
        private boolean hasFocus = false;
        
        public ArrowLabel() {
            super(">", Field.FOCUSABLE);
            lblFont = getFont().derive(26);
            setFont(lblFont);            
        }
        
       
        protected boolean navigationClick(int status, int time)
        {
            fieldChangeNotify(1);
            return true;
        }
        
        protected boolean touchEvent(TouchEvent message) {
            try {
                if (message.getEvent() == TouchEvent.CLICK) {
                    if (message.getY(1) < 0)
                        return false;
                    if (message.getY(1) > getHeight())
                        return false;
                    if (message.getX(1) < 0)
                        return false;
                    if (message.getX(1) > getWidth())
                        return false;
                        
                    // do your code
                    return true;
                } else if (message.getGesture().getEvent() == TouchGesture.TAP) {
                    if (message.getY(1) < 0)
                        return false;
                    if (message.getY(1) > getHeight())
                        return false;
                    if (message.getX(1) < 0)
                        return false;
                    if (message.getX(1) > getWidth())
                        return false;
                    setFocus();
                }
            }catch(Exception e) {
                System.out.println("Excep - " + e.getMessage());
                e.printStackTrace();
            }
            return false;
        }
        
        protected void onFocus(int direction) {
            //setFont(getFont().derive(Font.BOLD));
            hasFocus = true;
        }
        
        protected void onUnfocus() {
           // setFont(lblFont);
           hasFocus = false;
           super.onUnfocus();
            invalidate();
        }
        
        public void paint(Graphics graphics) {   
            if (hasFocus) {
                graphics.setFont(getFont().derive(Font.BOLD));
            } else {
                graphics.setFont(lblFont);
            }
            super.paint(graphics);
        }
    }   // End of class ArrowLabel

 

 

Thanks


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Problem with CustomLabelField

        protected boolean touchEvent(TouchEvent message) {
try {
if (message.getEvent() == TouchEvent.CLICK) {
if (message.getY(1) < 0)
return false;
if (message.getY(1) > getHeight())
return false;
if (message.getX(1) < 0)
return false;
if (message.getX(1) > getWidth())
return false;

// do your code
return true;
} else if (message.getGesture().getEvent() == TouchGesture.TAP) {
if (message.getY(1) < 0)
return false;
if (message.getY(1) > getHeight())
return false;
if (message.getX(1) < 0)
return false;
if (message.getX(1) > getWidth())
return false;
setFocus();
//write your field change code here
return true;

}
}catch(Exception e) {
System.out.println("Excep - " + e.getMessage());
e.printStackTrace();
}
return false;
}

try like this please

with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem with CustomLabelField

 

Zany, I have 3 ArrowLabel fields added in my code. Code for 3 of them are different.

 

How can I know that which field among the 3 was pressed, as I have to execute the code accordingly ?? At present I just added Dialog.inform ("Touch trapped" ) & ("Tap trapped") & its working perfect. But Ishould knoe the fieldname as to execute the code accordingly?

 

Any suggestions ?

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Problem with CustomLabelField

I think you are handling the touch events in component level. is it right? if so, then you can have an index in your custom component (ArrowLabel) class. In the constructor, you can assign them and based on the index, you can handle your touch events for appropriate component

 

code snippet:

 

class ArrowLabel extends LabelField

{

    private int _index;

 

    public ArrowLabel (int index)

    {

         _index = index;

    }

 

class ArrowLabel extends LabelField

{

    private int _index;

    public ArrowLabel (int index)

    { _index = index;

    }

protected boolean touchEvent(TouchEvent message) {
try {
if (message.getEvent() == TouchEvent.CLICK) {
if (message.getY(1) < 0)
return false;
if (message.getY(1) > getHeight())
return false;
if (message.getX(1) < 0)
return false;
if (message.getX(1) > getWidth())
return false;

// do your code
switch (_index)
{
case 1:
System.out.println ("First Arrow label in clicked");
break;
                       case 2:
                             System.out.println ("Second Arrow label in clicked");
                             break;

                       case 3:
                             System.out.println ("Third Arrow label in clicked");
                             break;

}
return true;
} else if (message.getGesture().getEvent() == TouchGesture.TAP) {
if (message.getY(1) < 0)
return false;
if (message.getY(1) > getHeight())
return false;
if (message.getX(1) < 0)
return false;
if (message.getX(1) > getWidth())
return false;
setFocus();
                    switch (_index)
                    {
                       case 1:
                             System.out.println ("First Arrow label in tapped");
                             break;
                       case 2:
                             System.out.println ("Second Arrow label in tapped");
                             break;

                       case 3:
                             System.out.println ("Third Arrow label in tapped");
                             break;

                    }

return true;
}
}catch(Exception e) {
System.out.println("Excep - " + e.getMessage());
e.printStackTrace();
}
return false;
}
with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Highlighted
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem with CustomLabelField

 

Thanks zany, this idea really helped me. I also tried adding rh actual code of fieldListener, & it worked perfectly well. But whenever I click or tap, I still see NullPointerException :

 

TOUCH EVENT Excep - java.lang.NullPointerException
No stack trace
TOUCH EVENT Excep - java.lang.NullPointerException
No stack trace
Arrow 1 Tapped
JVM: bklt @7896: timer
JVM: bklt[1] @7896: idle 1
JVM: bklt @7896: setTimer 21
TOUCH EVENT Excep - java.lang.NullPointerException
No stack trace
TOUCH EVENT Excep - java.lang.NullPointerException
No stack trace
Arrow 1 Clicked

 

Whole code of touchEvent is under try.. catch

catch(Exception e) {
                System.out.println("TOUCH EVENT Excep - " + e.toString());
                e.printStackTrace();
            }

And on doing a single click or single tap, I receive this error. Though the code works perfectly well, can't know what is making this error throw ? Can you guess any thing of ?

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.