Getpreferredwidth and getpreferredheight custom text field

I have a customtextfield that I have to get the width of textfields, I tried getpreferredwidth() substitution but I'm stacoverflowmethod, I can understand the reason for the error but I tried getwidth() substitution I get 10 as standard response. I am not able to understand the error.

public class CustTextField extends RichTextField{

    private String _text;
    private FontFamily _fontFamily1;
    private int _size, _color;
    private Font _headFont = null;

    public CustTextField(String _text, int _size,int _color, long _property)
    {
        super(_text, _property);
        FontFamily _fontFamily1;
        this._color = _color;
        try{
            _fontFamily1 = FontFamily.forName("aerial");
        } catch(ClassNotFoundException e) {
                _fontFamily1 = Font.getDefault().getFontFamily();
        }
         _headFont = _fontFamily1.getFont(Font.PLAIN,_size);
         super.setFont(_headFont);
    }

    public int getPreferredWidth() {
        // TODO Auto-generated method stub
        return getPreferredWidth();
    }

    protected void paint(net.rim.device.api.ui.Graphics g) {
        g.setColor(_color);
        super.paint(g);
    }
}

A Y

You can use font.getAdvance

Tags: BlackBerry Developers

Similar Questions

  • 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);
                }
            }
    
        }
    }
    
  • 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.

  • 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.

  • Why have I not the properties stocks, Format, validate and calculate for text fields in Acrobat DC?

    I'm looking into buying acrobat dc but note that when you make a form, text fields is more options above when property a right click. This removed the DC?

    No, he's still here.

    Try this: click Tools - prepare the form and then click on "More" to the right of the window, click "return to Acrobat Form.

  • 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; ".

  • 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

  • I have two text fields, where the user will sign a text field and the other text field should be mandatory

    I have 2 text fields, and how can I do a condition when the text 1 field is entered, then they must enter data into the text field 2.  I know it seemed simple, but I'm just a beginner in this... Thanks in advance!

    Hello

    I'm sorry, I forgot this matter.

    Please try this code...

    If (this.isNull)

    {

    TextField2.validate.nullTest = 'error ';

    }

    on the other

    {

    TextField2.validate.nullTest = "disabled";

    }

    Hope than that,

    S, candy.

  • 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 = "";

  • detect device language and change the text field accordingly

    Im working in Flash Pro with AIR for Android.

    I have a single text field and would like to change the language in this text field to match the language of mobile devices.

    How do I detect the language of devices, and then change the textfield object to match the language?

    import flash.system.Capabilities;

    switch (Capabilities.language)

    {

    case 'in ':

    TxtField.text = 'English LANGUAGE '.

    break;

    case "en":

    TxtField.text = "organizing";

    break;

    case "es":

    TxtField.text = "Tchequelangue";

    break;

    by default:

    TxtField.text = "Whatever";

    }

    Use Capabilities.languages [0] for the country code. for example. en-US

    switch (Capabilities.languages [0])

    //{

    case "en-US":

    TxtField.text = "English LANGUAGE. '" American ".

    break;

    case "en-GB":

    TxtField.text = "English LANGUAGE. Britain ";

    break;

    by default:

    TxtField.text = "Whatever";

    //}

  • Muse Business Catalyst and the customer text edition

    Can Muse Web sites cause editability of the customer text if published and hosted with catalyst for business?

    At the present time there is no client mount support Muse published sites. If you do not enable client Business Catalyst editing features, all changes are not compatible with the Muse. They will not appear in Muse and they can be replaced by future publishing operations of Muse.

    We will work to enable the functionality of the client update in future updates of Muse, but he will not be part of the next quarterly update.

    Currently, the box in British Colombia that allows you to activate the client editing feature is useful if the workflow should be only a means. A site is created and republished in Muse, then never updated day/re-published Muse (as this could therefore overwrite any changes have been made using the client edition feature).

  • 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

  • Fill a field with the results of the other (sort and add custom text)

    I have a PDF form with many fields popup asking 'Yes', 'Maybe' or 'no '. I need a way to collect the answers and sorting, all the 'yes' first of all, that all the 'maybe', all the 'no '. When I need after that sort of response of three, is put in a 'Results' field with the container of the ToolTips for each popup and the ability to add a "BOLD" text, between three sorted responses.

    I have found some javascript in the forum here to collect data from field only. It seems to be easy to modify if you have several fields to collect/sorting/extract.

    Assistance or management to do will be appreciated.

    Thank you!

    Assuming that you have 50 menus named A1, A2... A50 and each drop-down list has three elements (Yes, maybe, no), you could do something like:

    Initialize object to contain the info of the answer

    var oResponse = {}

    'Yes': [],.

    "Maybe": [],.

    ['No':]

    };

    var i, f;

    Browse fields (A1, A2... A50)

    for (i = 1; i<= 50;="" i="" +="1)">

    Get a reference to the current field

    f = getField ("A" + i);

    Add ToolTip text of the current field to the corresponding table in the response object

    oResponse [f.valueAsString] .push (f.userName);

    }

    Generate the output string

    aOutput var = [];

    If (oResponse ['yes'] .length > 0) aOutput.push ("It's the priority YES\r" + oResponse ["Yes"] .join ("\r"));

    If (oResponse ["Maybe"] .length > 0) aOutput.push ("It's the priority MAYBE\r" + oResponse ["Maybe"] .join ("\r"));

    If (oResponse ['No'] .length > 0) aOutput.push ("It's the priority NO\r" + oResponse ["No"] .join ("\r"));

    Enter the results

    getField("Results").value = aOutput.join("\r\r");

  • 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.

  • 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...

Maybe you are looking for