focus with custom text field problem

Hello

I am new to BB dev., trying to write a small program with two CustomTextFields, but faceing the problem, then try to verify that CustomTextField is selected (the focus).

CustomTextField (NumericTextField)

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;

public class NumericTextField extends Manager
{

    private final static int DEFAULT_LEFT_MARGIN = 10;
    private final static int DEFAULT_RIGHT_MARGIN = 10;
    private final static int DEFAULT_TOP_MARGIN = 5;
    private final static int DEFAULT_BOTTOM_MARGIN = 5;

    private final static int DEFAULT_LEFT_PADDING = 10;
    private final static int DEFAULT_RIGHT_PADDING = 10;
    private final static int DEFAULT_TOP_PADDING = 5;
    private final static int DEFAULT_BOTTOM_PADDING = 5;

    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;

    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;

    private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
    private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;

    private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
    private int width = Display.getWidth();
    private int height = minHeight;

    private EditField editField;

    public NumericTextField()
    {
        super(0);

        editField = new EditField(EditField.FILTER_REAL_NUMERIC);
        add(editField);
    }    

    protected void sublayout(int width, int height)
    {
        Field field = getField(0);
        layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
        setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
        setExtent(this.width, this.height);
    }

    public void setTopMargin(int topMargin)
    {
        this.topMargin = topMargin;
    }

    public void setBottomMargin(int bottomMargin)
    {
        this.bottomMargin = bottomMargin;
    }    

    protected void paint(Graphics graphics)
    {
        graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5);

        boolean longText = false;
        EditField ef = (EditField)getField(0);
        String entireText = ef.getText();

        String textToDraw = "";
        Font font = getFont();
        int availableWidth = width - totalHorizontalEmptySpace;
        if (font.getAdvance(entireText) <= availableWidth)
        {
            textToDraw = entireText;
        }
        else
        {
            int endIndex = entireText.length();
            for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
            {
                textToDraw = entireText.substring(beginIndex);
                if (font.getAdvance(textToDraw) <= availableWidth)
                {
                    longText = true;
                    break;
                }
            }
        }

        if (longText == true)
        {
            ef.setText(textToDraw);
            super.paint(graphics);
            ef.setText(entireText);
        }
        else
        {
            super.paint(graphics);
        }
    }

    public int getPreferredWidth()
    {
        return width;
    }

    public int getPreferredHeight()
    {
        return height;
    }

    protected boolean keyChar(char ch, int status, int time)
    {
        if (ch == Characters.ENTER)
        {
            return true;
        }
        else
        {
            return super.keyChar(ch, status, time);
        }
    }

    public String getText()
    {
        return ((EditField)getField(0)).getText();
    }

    public void setText(final String text)
    {
        ((EditField)getField(0)).setText(text);
    }
}

and in my screen content trying to check:

NumericTextField focusImput() {
    NumericTextField focusField;
    if (_fieldFrom.isFocus()) {
        focusField = _fieldtFrom;
    }
    else {
        focusField = _fieldTo;
    }
    return focusField;
}

but it always returns me _fieldTo...

NumericTextField definition:

_fieldFrom = new NumericTextField();
_fieldFrom.setChangeListener(this);
manager.add(_fieldFrom);

_fieldTo = new NumericTextField();
_fieldTo.setChangeListener(this);
manager.add(_fieldTo);

Maybe someone to guide me how to correctly focus feature?

Thanks in advance.

Then you need two slightly customized EditFields - their main customization would be limiting their width, as well as making them focusable/unfocusable. So, something like this should work:

public class NumericTextField extends EditField {
  // override the constructors used in your code, adding FILTER_REAL_NUMERIC
  // ...
  private int maxWidth = Integer.MAX_VALUE >> 1;
  private boolean focusable = true;

  protected void layout(int width, int height) {
    super.layout(Math.min(maxWidth, width), height);
  }

  public void setMaxWidth(int width) {
    maxWidth = width;
    updateLayout();
  }

  public boolean isFocusable() {
    return (isStyle(FOCUSABLE) & focusable);
  }

  public void setFocusable(boolean on) {
    focusable = on;
  }
}

If you want to place the fields, use setMargin - he is documented in OS 6.0 but works since OS 4.2.

Use setMaxWidth to, well, set desired maximum width (otherwise EditField tenorman to the top of the entire available width). Use the setFocusable to toggle the field. In addition, you can play with the Visual States for the field you turn, but it's an exersize for another day. First of all make sure that it works and you know how to use it.

Tags: BlackBerry Developers

Similar Questions

  • CUSTOM TEXT FIELD PROBLEM

    public class CustText extends TextField{
        private String _text;
        private FontFamily _fontFamily1;
        private int _size, _color;
        private Font _headFont = null;
    
        public CustText(String _text, int _size,int _color, long _property)
        {
            super(_property);
            this._text = _text;
            this._size = _size;
            this._color = _color;
        }
    
        protected void paint(Graphics g)
        {
            try{
                _fontFamily1 = FontFamily.forName("aerial");
            } catch(ClassNotFoundException e) {
                    _fontFamily1 = Font.getDefault().getFontFamily();
            }
            _headFont = _fontFamily1.getFont(Font.PLAIN,_size);
                g.setColor(_color);
            g.setFont(_headFont);
            g.drawText(_text,0, 0);
        }
    }
    

    I use the above code to customTextfield, the problem here is, when it is used to display long text that does not move to the next line, only the first line is painted, the rest of the lines are missing

    Graphics.drawText draws text on a single line.  This example allows input of multi line.

    http://supportforums.BlackBerry.com/T5/Java-development/create-a-custom-field-using-attributes-of-OT...

  • Problem to browse to files in the page with the text field with special characters

    Hello.
    I have a very interesting thing happening in my APEX (4.0.1) application.

    I have problems with a text field, because when I insert the text 'aaaaa' and send the page, the text is transformed in "Aaa£ aa.
    After some time trying to understand the game characters and translations and etc, I realized that this thing happened in some pages.

    Then, I started to remove components of the page where it happens and I discovered that what initially is an element to browse file...
    Every time I have send the page, if there is an element of browse file created on the page, the text is changed. If I delete the queue item browse, when submitting the page, the text remains correct.

    Is it supposed to work like this? And if yes, why is it?

    Thank you.
    vssantos

    Published by: vaissantos on April 1st, 2011 22:45

    Is it supposed to work like this?

    No, of course not.

    Of one of your previous posts, it seems that you use the APEX? If so, similar problems have been reported a number of times before: + {: identifier of the thread = 1104224} + suggests a fix.

  • Slider custom text field (Cap) does not appear

    All;

    I created a custom text field based on the standard text field, changes work fine (really just a custom height and width and a border).  The problem is the method of painting which seems to remove the cap of the field when it gets the focus.  To be clear the carret never shown at all.

    public class CustomText extends TextField //cutom text field width
    {
        //Custom Height and Width in Pixels
        final static int textwidth=125;
        final static int textheight=25;
    
         CustomText()
         {
         super(); // Call the default constructor    
    
         //Border for text fields
         XYEdges padding = new XYEdges(5, 5, 5, 5); //space between box and border
         Border theborder = BorderFactory.createSimpleBorder(padding, Border.STYLE_SOLID);
    
         //Assign the border to the object
         this.setBorder(theborder);
         }
    
         public void onFocus( int direction )
         {
            super.onFocus( direction ); //invoke parent procedure
            invalidate(); //mark for redraw
         }
    
         public void onUnfocus()
         {
        super.onUnfocus(); //invoke parent procedure
        invalidate(); //mark for redraw
         }
    
         //Override the layout with the custom Width and Height
         protected void layout(int width, int height)
         {
            super.layout(textwidth,textheight);
            setExtent(textwidth,textheight);
    
         }      
    
        //Override the display message such that no message is displayed when the field is full
        protected void displayFieldFullMessage()
        {
        }
    
        //Override the default paint method to call the super class constructor and then assign the border
        protected void paint(Graphics graphics)
        {
           // int prevColor = graphics.getColor(); //Save Previous Color
            graphics.setBackgroundColor(Color.LIGHTGRAY); // Set to Gray
            graphics.clear(); // Clear for redraw
            super.paint(graphics); //Force Redraw of Object
          //  graphics.setColor(prevColor); //restore for cursor paint
    
        }
    
    };
    

    In the paint in the first and last method lines that are commented out were a soloution that I tried after reading on this subject.  Something along the lines of the restoration of the default color, but unfortunately without success.   Any ideas would be more useful

    Thank you!

    Quick thoughts:

    (a) to remove the requirement to substitute paint by setting the background a Color.LIGHTGRAY using the base class

    (b) you put on the field by using this:

    Super.Layout (TextWidth, TextHeight);

    If you then do the following:

    setExtent (textwidth, textheight);

    you are likely to confuse the field.  If you still want to use the entire height and use the full width and then try to deal with it in the Style, that is, replace this:

    Super(); Call the default constructor

    with this

    Super(TextField.USE_ALL_HEIGHT |) TextField.USE_ALL_WIDTH); Call the default constructor

    (c) why you override onFocus and onUnfocus?  I would like to delete these if you do not have anything specific to these methods.

    Make these changes, and you'll have a more simple TextField.  See if these changes will solve the problem of the caret too.

  • Custom text field

    I want to implement a custom text field that displays its label on the leading edge of a line and its part editable on the TRAILING edge of the same way like a ChoiceField. Is there a documentation available to achieve this (maybe it's to say-sample code)? Or even if I could see the source code for ChoiceField. I develop for a Blackberry Pearl 8110 with 4.3.

    I ended up solve this using managers of horizontal inside vertical management. I have to use a label field for the edit field that accompanies it. When lines are added, I followed is the width of the new label. Then in sublayout to align the edit fields according to the maximum width more stored a Variant. Could not align all the way to the right.

    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.text.*;
    
    public class MyLayoutManager extends VerticalFieldManager {
        protected int maxw;
        protected Font font;
        protected int font_height;
        private int size;
    
        public MyLayoutManager() {
            super(Field.USE_ALL_WIDTH);
            this.maxw = 0;
            this.font = Font.getDefault().derive(Font.PLAIN, 12);
            this.font_height = this.font.getHeight();
            this.size = 0;
        }
    
        public void addLine(String label, int initial) {
            MyEditField mef = new MyEditField(label, initial);
            int label_width = this.font.getAdvance(label);
            this.maxw = Math.max(label_width, this.maxw);
            this.add(mef);
            this.size++;
        }
    
        public int getPreferredHeight() {
            return this.size * this.font_height;
        }
    
        public int getPreferredWidth() {
            return 240;
        }
    
        protected void sublayout(int maxwidth, int maxheight) {
            int y = 0;
            for (int i = 0; i < this.size; i++) {
                Field f = this.getField(i);
                this.setPositionChild(f, 0, y);
                this.layoutChild(f, 240, this.getPreferredHeight());
                y += this.font_height;
            }
            this.setExtent(240, this.getPreferredHeight());
        }
    
        protected class MyEditField extends HorizontalFieldManager {
            private LabelField the_label;
            private JunkField the_value;
    
            public MyEditField(String label, int initial) {
                super(Field.USE_ALL_WIDTH);
                this.the_label = new LabelField(label);
                this.the_label.setFont(font);
                this.the_value = new JunkField(initial + "");
                this.the_value.setFont(font);
                this.add(this.the_label);
                this.add(this.the_value);
            }
    
            public int getPreferredHeight() {
                return font_height;
            }
    
            public int getPreferredWidth() {
                return 240;
            }
    
            protected void sublayout(int maxwidth, int maxheight) {
                Field f = this.getField(0);
                this.setPositionChild(f, 0, 0);
                this.layoutChild(f, maxwidth, maxheight);
                f = this.getField(1);
                this.setPositionChild(f, maxw + 20, 0);
                this.layoutChild(f, maxwidth, maxheight);
                this.setExtent(maxwidth, font_height);
            }
    
            private class JunkField extends EditField {
                private boolean firstFocus;
    
                public JunkField(String deflt) {
                    super(Field.EDITABLE | Field.FOCUSABLE);
                    this.setText(deflt);
                    this.setFilter(new NumericTextFilter(TextFilter.NUMERIC));
                    this.firstFocus = false;
                }
    
                protected void onFocus(int direction) {
                    this.firstFocus = true;
                }
    
                protected boolean keyChar(char key, int status, int time) {
                    if (this.firstFocus) {
                        this.setText("");
                        this.firstFocus = false;
                    }
                    return super.keyChar(key, status, time);
                }
            }
    
        }
    }
    
  • Custom with LostFocus list field problems

    Hello experts!

    I made a custom, list field when I draw the lines, I made a solution using a personalized reminder, well... I have a problem when the list field lost focus, because when I have a focus to the button my list have a last item carried.

    I want to erase,

    Any suggestions?

    Best regards, Rampelotti

    in the drawListRow() method adds the underside before the draw.

    if(graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
    
  • Please help me with the custom text field FormCalc

    There is someone online has done a very good thing for me - they helped me create a text field that would be of type 'this document has been printed on _' and include some day the pdf was printed on the employer's intranet site.

    I had a shortcut in my favorites and would click on it and place it on each new document that I needed on.

    Because the computer I was using suddenly fell down, can't get the details of how to set it up again. I have a pdf form that includes the box, but when I open it in LiveCycle, it's just empty.

    That's what I saved:

    < field h = mm "8,4935" name = "PrintedOn" w = "141,0398 mm" x = "-0,0001 mm" y = "0 mm" xmlns ="http://www.xfa.org/schema/xfa-template/2.5/" > ""
    < ui >
    < textEdit >
    < border hand = 'right' presence = "hidden" >
    <? templateDesigner styleId aped0? > < / border >
    < margin / >
    < / textEdit >
    < /UI >
    < police size = cast "18pt" = "Tahoma" weight = "bold" >
    < filling >
    < color value = "221,221,221" / >
    < / filling >
    < / make >
    < para hAlign = "center" vAlign = "middle" / >
    < link match = "none" / >
    < activity = "prePrint" ref = "$host" >
    < script >$ .rawValue = concat ("this document has been printed on:", num2date (date (), DateFmt (1))); < /script >
    < / event >
    < / field >

    I don't remember what to do to get this working.

    I use this text box "print on" ALL the time, and I need to know what to do to get back to work quickly.

    If there is someone who could help me, I could also send you a sample of a form which includes it - so you can see exactly what I mean.

    If someone could remember how to program this kind of thing in once again and save it in LiveCycle, then explain how I can put it on another computer so I'd really, REALLY appreciate it.

    ~ Chris

    PS - this one as I had put in place only prints print message per day on the first page. If she could somehow print on EACH page of the PDF file without me having to click and add it to each page individually, it would be better... but let's first!

    Hello

    The script is here. If you place a new textfield in the Master Page and put the following script in the event of pre-publication of the field:

    $ = concat("This document was printed on: ", num2date(date(), DateFmt(1)))
    

    The language is FormCalc.

    Please note that you don't need to go to the XML Source for this. Simply select the textfield object and open the Script Editor (in the Windows menu). Drag the bottom of the editor bar so that you can see a few lines, and then set the FormCalc language.

    Should work,

    Niall

  • FlowFieldManager not 'compatible' with the text fields?

    Hi all.

    I'm making an icon to follow the end of a given text. A bit like the phoneLog done with the green phone icon next any number that you enter.

    I tried the substitution of the DrawText, but unfortunately after conditioning to the new line I don't have an X coordinated to begin to draw.

    That's what I would like to:

    +-----------++-----+

    | text 1234 | icon |

    +-----------++-----+

    After entering the text more:

    +--------------------++-----+

    | text 1234567890 | icon |

    +--------------------++-----+

    Unfortunately FlowFieldManager encapsulates the new line icon

    It seems that she's done this before and after each text field. If I have two icons, one after the other, they have a format horizontal, but at the moment where I have to add a textField, it is wrapped in a new line (are complemented by the icons) or icons are pushed to the next line (if the text box before the icons).

    So, how can I add this icon to track the length of the text?

    The problem here is that text fields normally tell their manager encompassing that they want to take the entire width.  FlowFieldManager who will respect and give them the entire width.

    You will need to customize the text field, replace the functions of layout and width and make sure he asks only the size that he actually need.

    If the text cannot be updated, and then you try to use rather than RichTextField LabelField.  LabelField asks only the width he needed.

    Sorry I can't give you any appropriate code because I have never done this.  I did not because from a user interface point of view I don't think it looks good.

    This mechanism can fail when the text field resumed more size on a width - fields are always "rectangular".

  • Error when creating custom text field

    HI Guyz,

    I am getting below error while creating a new custom UDF text field. All the world is facing this problem earlier?

    "Houston-COLUMN_UNAVAILABLE: no additional custom attributes of this type cannot be created"

    Swati - PÉAN

    The problem is finally solved.

    Yes, there are several udf to IOM. However, it stores the details in a table of the adf. Cleaning of the custom fields were created.

    TRUNCATE TABLE ADF_EXTENSION_COLUMN_USAGE;

    DELETE ADF_EXTENSIBLE_TABLE_USAGE;

    COMMIT;

    -Swati Pandey

  • Custom text fields

    How to make a text field that starts with "Dear," pulls a name to a different field, then ends with a comma?

    If the name of the other field is 'Name', say, you can use this code as the custom code of the field:

    var name = this.getField("Name").value;

    If (name) event.value = 'Expensive' + name + by ', ';

    else event.value = "";

  • refer to his car in custom text field calc

    I have a custom for a text field calculation, but I would like to know if it is possible to set the field itself as a variable without having to refer to the field name.  EITHER this.getField ("name") works, but I want to use this same script a number other text fields and do not want to customize each one.

    To get a reference to the field to which the script is attached, the field object is the value of event.target, so the code could be simplified as follows:

    // Custom Calculate script
    event.target.readonly = getField("Set 1").value === "Off" ? true : false;
    

    I changed to the analysis with a value of "Off", because the script didn't need to know what is the value of exports from the box. A deselected checkbox will have a value of "Off". This makes the script more generic.

  • Set focus to the text field

    Hello...

    Alread search on the forum but I can't find my answer...

    I need my text field early with a focus when my page load... How can I do? West an attribute apex or must be in javascript? If anyone can help with this?

    tnks to all

    If your textitem is the first element on your page that you can set in the properties of the page

    Display the attributes > Cusror Focus > for an object on the Page

    If this is not the case, try to add to the footer of your page:

    html_GetElement ('P1_TEXTFIELD'). Focus();

    Where you need to change P1_TEXTFIELD in the id of the item that you want to set the cursor focus.

    Denes Kubicek
    ------------------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    ------------------------------------------------------------------------------

  • Format custom text field only when the field is filled

    I have created a form that I need to have the format of a text field "model:"& value of text field, then when it is empty so it is just empty.»

    I put event.value = "template:" + event.value; "." in the format custom properties of the text field it set to format correctly, but if it had not entered in this field he said always model: so what should I do for it to be only occur when there is something entered in the field.

    Change to:

    If (event.value) event.value = "template:"+ event.value; ".

  • How to set the focus to a text field.

    Hello
    I have a form that contains 5 textfields. When the form is loaded, by default, the cursor should be on the second field. How can I concentrate on the text field.
    I checked in the API of the TextField object, and it is observed that the focusedProperty is a read only field.

    Can someone let me know how to focus on the desired TextField. ?

    Thanks in advance.
    The SAI Pradeep.

    Use textField.requestFocus)

    If your node is not always lay out to the point because of a management of the default focus of the javafx platform, wrap the call for development in Platform.runLater

  • iOS text field problem.

    I need the user to enter a Word, so I added a simple text field to the stage. However, it behaves strangely on the real device (iPod Touch). Attached are two screenshots that show how the font size increases when the input is activated. Everyone knows about this problem?IMG_0013.PNGIMG_0014.PNG

    Hello

    I think that you have specified a textFormat to text. You must add the following

    Assuming that this is the format

    var txtFormat:TextFormat = new TextFormat();

    txtFormat.size = 20;

    T1 = new TextField();

    T1.defaultTextFormat = txtFormat;

    Apply the default text format that we just put in the text that is already in the text field ('g')

    Otherwise the default text format apply to * new * text * typed * in the text field!

    Without this, the original text in text fields ('g') will use the default value for a

    Flash text (Times New Roman 12pt), rather than the defaultTextFormat (Times New Roman 20pt) field.

    T1.setTextFormat (T1.defaultTextFormat);

    addChild (t1);

    This affects any existing text in the text field in the specified format (making it the same as the default value). Now, when the text field is rendered first uses the default format that you set, and when you type also uses this same format, and so the format is no longer able to change.

    I hope this helps.

    Samia

Maybe you are looking for

  • 10.11.4 OSX update itunes library is broken

    After the last update of El Capitan, iTunes can not find more my library. It is located on an external hard drive, it is still there but it seems that the file format changed in the latest version of itunes so that it can not find the library more. A

  • MacBook pro 13' (mid-2014): can I use a cooling base...

    Can I use a base of cooling taking air from beneath and bring it to sides? I don't know where my macbook take fresh air of, the sides or the front? However, I have seen that there are 2 types of basic cooling, who take on sides and fresh air under th

  • Compaq Presario CQ57-104TU: upgrade Ram, memory (Ram Hynix PC3L-12800)

    Hello world I have a Compaq Presario CQ57-104TU. I recently decided to upgrade the ram (currently 1 x 2 GB Elpida PC3 - 10600 1333 Mhz). I bought 2 x 4 Gb Hynix HMT451S6AFR8A, PC3L-12800. Unfortunitely when I install the new Ram (Hynix) the computer

  • Explore Vista crashes when opening folder

    solved problem in Vista home - windows explored hangs when opening a folder. seemed to be caused by the thumbnail view but down sometimes happens in the list or details view. While some suggest, find a codec download, my problem was solved by removin

  • New hard drive for compaq presario c500 running vista. I want to switch to windows 7

    I'm fixing my laptop aunts. He a presario c500 compaq running windows vista. I bought a new hard drive. I would like to install windows 7. She could get windows 7 discs for free when she got off the computer, windows 7 was not yet. They have offered