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
Developer
ChristianH
Posts: 15
Registered: ‎02-11-2009
My Device: Not Specified
Accepted Solution

BasicEditField NO_NEWLINE not working?

I have a BasicEditField where I want the Enter key ignored. I am attacking this two ways to make sure I get it right. First, I pass NO_NEWLINE into the constructor. Second, I intercept the ENTER key in keyChar() and simply return. Basically I have replicated the CustomKeywordField from the KeywordFilterField example. However, the Enter key is not ignored (atleast in the simulator). I space is added after the last character typed. Is this expected behavior? Is there something I am doing wrong? Here is my class for completeness.Thanks for the help.

 

   final static class CustomKeywordField extends BasicEditField
    {   
        // Constructor
        CustomKeywordField()
        {
            // Custom style.
            super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE);
            
            setLabel("Search: ");
        }
        
        protected boolean keyChar(char ch, int status, int time)
        {
            if (ch == '\n')
                return true;
            
            switch(ch)
            {
                case Characters.ESCAPE:
                    // Clear keyword.
                    if(super.getTextLength() > 0)
                    {
                        setText("");                        
                        return true;
                    }
                case Characters.ENTER:
                    return true;
                
            }                
            return super.keyChar(ch, status, time);
        }                     
        
        /**
         * Overriding super to add custom painting to our class.
         * @see net.rim.device.api.ui.Field#paint(Graphics)
         */
        protected void paint(Graphics graphics)
        {            
            super.paint(graphics);
            
            // Draw caret.
            getFocusRect(new XYRect());
            drawFocus(graphics, true);                          
        }
    }

Developer
Rajat_10Sep
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: BasicEditField NO_NEWLINE not working?

Hi,

 

I think you should use KeyListener interface and it methods keyDown(),keyUp() etc .

 

return false from methods if you donot want to event get consumed.

 

When enter key is pressed you can catch that event in keyChar() which passes key with it as one of the parameter and then you can make your method return like this

 

public boolean keyChar(char key, int status, int time){

        return false;
     }

 

 

Please let me know did this solve your problem or not.

 

Regards,

Rajat Gupta.

--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: BasicEditField NO_NEWLINE not working?

[ Edited ]

Don't worry about a keyListener, your code should work fine as is.  I have similar code, will just check it out. 

 

I'm a bit concerned about this:

if (ch == '\n')

What is it in there for?

 

Edit:  just checked my code and tested, keyChar is not invoked if the Field has been defined using BasicEditField.NO_NEWLINE and the user presses the <enter> key.

 

if you are seeing keyChar being invoked, can you confirm that you are using BasicEditField.NO_NEWLINE and which JDE/OS Level you are seeing this on.

Message Edited by peter_strange on 03-09-2009 07:38 PM