OneLineTextField + CustomButtonField

Hi all

On my screen, I have a HorizontalFieldManager which has a taken from OneLineTextField

http://supportforums.BlackBerry.com/T5/Java-development/sample-code-scrollable-one-line-text-input-f...

and a custom button field. The problem comes when I have to scroll among them. First scrolling did not work properly, when I tried to move to the right of the TextField to the key, that the last of them was never. I solved this dominant navigationMovement in the HorizontalFieldManager. So now works fine, but the problem comes when I move to the right of the inputText to the button and there is an already typed text. Instead of spending between the text, it always comes to the button on the right (which is obvious after the substitution of navigationMovement, but is is not the behavior that I would have).

I was wondering if there is any way to solve this kind of issue, namely when move the focus to the button and when accommodation and simply scroll the text.

Thanks in advance for your help.

See you soon

EDIT: I forgot to say that I only have this problem in a normal device. Simulator, everything works great, I did not care about the change in emphasis, adding just the oneLineTextField and my custom with a HorizontalFieldManager buttonField and simply works great! Focus problem when I test this on a real device, and that's why I tried the substitution of navigationMovement...

Sorry, my mistake. There was an error and that I was wrongly using managers who ran the fields. Just replaced the labelField that comes in the code example with my field of custom and all button works as expected.

Tags: BlackBerry Developers

Similar Questions

  • OneLineTextField and the border are not displayed correctly together

    Hi all, I need a container by tabs and found the example of the pill buttons that look awesome

    and can function as tabs, (from the article 'Implement advanced buttons, fields, and managers')

    so I use in my application.

    Let me say, they are superb, thank you very much for creating that

    then in one of the 'tabs', I asked

    a field of research (OneLineTextField), followed by the results (ObjectListField) below

    but my field of research is not get drawn properly (see photo)

    seems the border intersect on the right and try to reduce its size

    seems to be ignred. I'm under api 5 with 5.0.0519 Simulator (or is it 591)

    any help on how I can get this to look like, I expect, in addition, also, where is the

    stress being placed in OneLineTextField put, I guess that this is when he thinks a

    misspelled word is there.

    I would like to show a remove this effect and just get to lok more professional

    OneLineTextField searchOneLineTextField = new OneLineTextField ("search for:", "");
    XYEdges padding = new XYEdges (2,2,2,2);
    At the border roundedBorder = BorderFactory.createRoundedBorder (padding, Border.STYLE_SOLID);

    searchOneLineTextField.setBorder (roundedBorder);
           
           
    Add (new LabelField ("screen test"));
    Add (new SeparatorField());
    Add (searchOneLineTextField);

    you are great here, without being able to read through it, I would never get this app for my complete work

    FYI: I had the OneLineTextField from here

    http://supportforums.BlackBerry.com/T5/Java-development/sample-code-scrollable-one-line-text-input-f...

    and the example of "implement advanced buttons, fields and managers" of

    http://supportforums.BlackBerry.com/T5/Java-development/implement-advanced-buttons-fields-and-manage...

    u blackberry developers are more helpful engineers and sweet, with that I've never dealth.

    I'm waiting for a sarcastic response while I read all these

    positions and never a response brief soaking, great work team.

    your solution worked like a champ

  • Click here for customButtonfield to consume

    I created a button field custom, which works, but menu appears when you click the button, my custom button field extends the scope only.

    no idea how to block the menu?

    Concerning

    Rakesh shankar. P

    Hi rakesh,

    I think that you are working in the storm. Have you tried FieldChangeListener.if no first then try this...

    If it dint work.

    Try like this

    protected boolean navigationClick (int status, int time) {}

    GotoLink();
    Returns true;
    }
    protected boolean touchEvent (TouchEvent event) {}
    int eventCode = event.getEvent ();
    If (eventCode == TouchEvent.CLICK) {}
    GotoLink();
    Returns true;
    }
    Return super.touchEvent (event);
    }

    private void GotoLink() {}
    If (UiApplication.getUiApplication () .getActiveScreen () .getLeafFieldWithFocus () instanceof CustomButtonField) {}
    Final CustomButtonFieldfocus = (CustomButtonField) UiApplication.getUiApplication () .getActiveScreen () .getLeafFieldWithFocus ();
    If ((attention! = null) & (focus instanceof CustomButtonField)) {}

    / * if(focus == CustomButtonField1) {}

    } else if * / / / code here...

    }

    }

    }

    Thank you & best regards

    pp

  • CustomButtonField fieldChanged

    This page seems to describe what I do, hoping someone can help me with more details:

    http://rim.lithium.com/T5/BlackBerry-App-world-development/click-event-on-the-custom-button/m-p/2943...

    So I have a CustomButtonField:

    SubmitButton CustomButtonField = new CustomButtonField ("Submit", Color.LIGHTGREY); 65,112,160);
    submitButton.setChangeListener (this);

    Then, within the same HorizontalFieldManager, I have two standard ButtonFields.

    I have a function fieldChanged:

        public void fieldChanged(Field field, int context) {
    
            if (field == submitButton) {
                submitStart();
            }
            else if (field == testButton) {
                testStart();
            }
        }
    

    If I click on my CustomButtonField, the

    if (field == submitButton) {
    

    is NOT true, then he never calls submitStart();

    If I click on my standard ButtonField testButton, that if this is true and that it runs testStart() as expected.

    Looks like the link above suggests either, I need to have another function fieldChanged specially to my CustomButtonField or I need to access the instance.  I don't know exactly how to start on one of these solutions.

    Can you please confirm that when your submitButton is "pressed", that your FieldChanged method is called.  I presume that it is.  In this case, I could absolutely confirm the scope and status of the submitButton object that you create with:

    SubmitButton CustomButtonField = new CustomButtonField ("Submit", Color.LIGHTGREY); 65,112,160);

    I do not see the code you provided we default unless you have multiple instances of the submitButton, add a different button or update this reference somewhere else in your program, perhaps with another

    submitButton = new CustomButtonField (...);

    To put on the lines break points

    submitButton.setChangeListener (this);

    If (field == submitButton) {}

    and make sure your reference points submitButton to the same object in two different points.

  • CustomButtonField + Capture hold click?

    Hi, I wonder how I can know when is a stamped click (and hold) in the button, the button with a highlight color for painting.

    Thank you

    Okay, market... with

    navigationClick, navigationUnclick... and other events like the touch, etz

    fieldchangelistener.
    or navigationclick/touchevent

  • Implementation CustomButtonField as a backdrop in the black berry

    can someone tell me how to set the custom button as a backdrop to black Berry field.

    You cannot set a ButtonField as a background. If you try only to use an image, see the BackgroundFactory class. Also be aware that the definition of the background on a screen in general does not work - you will need to set the background to a VerticalFieldManager and then add it to the screen.

  • CustomListField + CustomButtonField + touchEvent + NavigationClick is HELL

    Hello world

    I'm doing this thing works

    I have a screen which consists of three components

    HEADER

    -a custom refresh button (extends the scope with a bitmap and override touchEvent and navigationClick)

    CONTENT

    -NavigationClick and custom touchEvent ListField and substitution

    FOOTER

    -Type of a menu with several button (application as well as for the header)

    When I run my screen, the focus is made on the footer, so I can use my buttons

    If I click on the listField or even make dragging, then the listField component get the Focus.

    Now, when I touch one of my footer button, only the method of the ListField touchEvent's launch, even if I return false in everycase, neither the toucheEvent nor my footer buttons navigationClick method will be called.

    So if I click on my footer, I see only the listField slips a little and nothing else.

    Anyone experienced the same thing?

    This BlackBerry bug has already been reported and confirmed by RIM on this forum:

    ListField scrolling behind other fields

    So no, you're not crazy, and no, you do not have something wrong.  I hope it's a consolation

  • Refresh the field label

    Hi, I read many posts on the forum but I have not found any that solved my problem.

    I'm new to the development of BB and I'm having a problem that I seem not to be able to untangle.

    I have a main class and a class of the screen. I have a button that launches a datepicker. When the DatePicker is closed I need the label of the button to refresh with the selected value. How should I do?

    I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus and some other stuff, but I guess I have them implemented in a bad way...

    My two classes are these...

    public class TestClass extends UiApplication
    {

    public static calendar alarm1time = Calendar.getInstance (TimeZone.getTimeZone("GMT-3"));

    Public Shared Sub main (String [] args)
    {

    TestClass testClass = new TestClass ();
    testClass.enterEventDispatcher ();

    }

    Public TestClass()
    {

    pushScreen (new TestClassScreen());

    }

    }

    / public final class TestClassScreen extends screen
    {

    public TestClassScreen()
    {

    ButtonField alarm1 = new ButtonField ("alarm:" + TestClass.alarm1time.get (Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get (Calendar.MINUTE), ButtonField.FOCUSABLE)

    {

    public boolean navigationClick (int status, int time)
    {

    datePicker (1);
    Returns true;

    }

    };

    setTitle ("Test alarm");
    Add (new RichTextField(""));
    Add (alarm1);

    }

    public void datePicker (mutable int alarmNumber)
    {

    UiApplication.getUiApplication () .invokeLater (new Runnable()
    {

    public void run()
    {

    DateTimePicker datePicker = null;
    datePicker = DateTimePicker.createInstance (TestClass.alarm1time, null, "HH: mm");
    If (datePicker.doModal ())
    {

    Calendar cal = datePicker.getDateTime ();
    TestClass.alarm1time = cal.

    Here I need the label to be updated, in the end the datePicker is Ok

    }

    }

    });

    }

    }

    Thank you

    Alex

    Welcome to the forums.

    Here's one way:

    public boolean navigationClick (int status, int time)
    {

    datePicker(1, this);
    Returns true;

    }

    Public Sub switch of dates (alarmNumber final int, ButtonField buttonToUpdate final)
    {

    UiApplication.getUiApplication () .invokeLater (new Runnable()
    {

    public void run()
    {

    DateTimePicker datePicker = null;
    datePicker = DateTimePicker.createInstance (TestClass.alarm1time, null, "HH: mm");
    If (datePicker.doModal ())
    {

    Calendar cal = datePicker.getDateTime ();
    TestClass.alarm1time = cal.

    buttonToUpdate.setLabel (...)}

    }

    });

    }

    A more usual way of being having change listener listen to click on the button and perform a similar treatment in there.

    Edit: The Code has been corrected.

  • Problem with fieldChanged() and custom button field

    Hello

    I created a custom button class by extending LabelField.  I chose LabelField over field because the LabelField contains desirable properties that are already being implemented.  The only problem I'm having has to do with the change listener.  It seems to 'steal' the event click on other areas in my application.

    For example, when I click on the custom button, a popupscreen with a listfield opens. When I click on an item in the listfield, then the fieldChanged() of custom button is called again...

    Can you see anything wrong with my code?

    package com.rantnetwork.fields;
    
    import com.rantnetwork.app.Constants;
    import net.rim.device.api.system.Display;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Ui;
    import net.rim.device.api.ui.XYEdges;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.decor.BackgroundFactory;
    import net.rim.device.api.ui.decor.BorderFactory;
    
    public class CustomButtonField extends LabelField {
    
        private boolean highlighted = false;
    
        public CustomButtonField(String text, long style) {
            super(text, style | Field.FOCUSABLE | LabelField.ELLIPSIS);
    
            setPadding(10, 0, 10, 5);
    
            setFont(Font.getDefault().derive(Font.BOLD,
                    Constants.DEFAULT_FONT_SIZE, Ui.UNITS_pt));
    
            setBackground(BackgroundFactory.createLinearGradientBackground(
                    0x163d7c, 0x163d7c, 0x03162d, 0x03162d));
            setBorder(BorderFactory
                    .createBevelBorder(new XYEdges(1, 1, 1, 1), new XYEdges(
                            Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK),
                            new XYEdges(Color.BLACK, Color.BLACK, Color.BLACK,
                                    Color.BLACK)));
    
        }
    
        public int getPreferredWidth() {
            return Display.getWidth() / 3;
        }
    
        protected void paint(Graphics graphics) {
            graphics.setColor(Color.WHITE);
            super.paint(graphics);
        }
    
        protected void drawFocus(Graphics graphics, boolean on) {
            // Do nothing
        }
    
        protected boolean navigationClick(int status, int time) {
            fieldChangeNotify(1);
            return true;
        }
    
        protected void onFocus(int direction) {
            if (!highlighted) {
                setBackground(BackgroundFactory.createLinearGradientBackground(
                        0x4bb7df, 0x4bb7df, 0x1b96da, 0x1b96da));
                setBorder(BorderFactory.createBevelBorder(new XYEdges(1, 1, 1, 1),
                        new XYEdges(Color.BLACK, Color.BLACK, Color.BLACK,
                                Color.BLACK), new XYEdges(Color.BLACK, Color.BLACK,
                                Color.BLACK, Color.BLACK)));
            }
        }
    
        protected void onUnfocus() {
            if (!highlighted) {
                setBackground(BackgroundFactory.createLinearGradientBackground(
                        0x163d7c, 0x163d7c, 0x03162d, 0x03162d));
                setBorder(BorderFactory.createBevelBorder(new XYEdges(1, 1, 1, 1),
                        new XYEdges(Color.BLACK, Color.BLACK, Color.BLACK,
                                Color.BLACK), new XYEdges(Color.BLACK, Color.BLACK,
                                Color.BLACK, Color.BLACK)));
            }
        }
    
        public void showHighlighted(boolean focus) {
            if (focus) {
                highlighted = true;
                setBackground(BackgroundFactory.createLinearGradientBackground(
                        0x4bb7df, 0x4bb7df, 0x1b96da, 0x1b96da));
                setBorder(BorderFactory.createBevelBorder(new XYEdges(1, 1, 1, 1),
                        new XYEdges(Color.BLACK, Color.BLACK, Color.BLACK,
                                Color.BLACK), new XYEdges(Color.BLACK, Color.BLACK,
                                Color.BLACK, Color.BLACK)));
            } else {
                highlighted = false;
                setBackground(BackgroundFactory.createLinearGradientBackground(
                        0x163d7c, 0x163d7c, 0x03162d, 0x03162d));
                setBorder(BorderFactory.createBevelBorder(new XYEdges(1, 1, 1, 1),
                        new XYEdges(Color.BLACK, Color.BLACK, Color.BLACK,
                                Color.BLACK), new XYEdges(Color.BLACK, Color.BLACK,
                                Color.BLACK, Color.BLACK)));
            }
            invalidate();
        }
    
        public boolean isHighlighted() {
            return highlighted;
        }
    
    }
    

    behrk2 wrote:

    Now, I'm not sure why customButton.setText (calling) would trigger the fieldChanged().  Can anyone think of a reason why he can do?

    Thank you!

    Can you think of a reason why we can't do that? The field has changed, after all! Of course, the context (second argument to fieldChanged) will be PROGRAMMATIC in this case, that might be a pretty good indication for you. But not invoke fieldChanged at all would be wrong.

    This is why I don't like the idea of extending LabelField and not just the field for your custom badges - you have much less control over his behavior. If you want an example showing how to create abstract off-screen buttons, take a look at BaseButtonField and his descendants in managers, fields and advanced buttons.

  • Align ButtonField Center in HorizontalFieldManger when ObjectChoceField is not selected

    Hello
    I have developed a screen in which there's a topic labelField below that there is an ObjectChoiceField... My position and ObjectChoiceField do not scroll and will remain constant, under the title and ChoiceField object I have some data that must be the subject of a scroll and I could achieve this scrolling thing but my problem is that I place a button below these materials and placed this button in HorizontalFieldManager as follows

    ButtonField back;
    HorizontalFieldManager But_Back = new HorizontalFieldManager (HorizontalFieldManager.FIELD_HCENTER);
    Back = new CustomButtonField ("Back", ButtonField.FIELD_HCENTER);
    Back.setChangeListener (this);
    But_Back.Add (back);

    The first problem: At when only the header of the loading of the page and object coice field and my left side coded hard LabelField will be present and the button below and when I select the value in the ObjectChoiceField then the values corresponding to the labelField letside will be displayed all I can attchieve using the fieldChanged but my button that is currently I am unable to put in the Center... that when the page is loaded without values are present, so the button is to the left side and when I select a value of ObjectChoiceField, then it is aligned to the Center, I want to be at the Center...
    FYI: in the order of scroll implememtn in my Constructori say

    Super (new VerticalFieldManager (), NO_VERTICAL_SCROLL);
    Here I put the code for coice position and object fields in two different HorizontalFieldManager and then announces to the screen
                 
    For the scrolling now, I create a verticalFieldManger like:
    VerticalFieldManager vor = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL |) VerticalFieldManager.VERTICAL_SCROLLBAR);
    and each variable left and right in addition to HorozontalFieldmanger
    HFM HorizontalFieldManager = new HorizontalFieldManager();
    VFM. Add (HFM);

    and at the end I say
    HorizontalFieldManager But_Back = new HorizontalFieldManager (HorizontalFieldManager.FIELD_HCENTER);
    Back = new CustomButtonField ("Back", ButtonField.FIELD_HCENTER);
    Back.setChangeListener (this);
    But_Back.Add (back);
    VFM. Add (But_Back);
    Add (VFM);

    Add Field.USE_ALL_WIDTH in your VerticalFieldManager and DrawStyle.HCENTER in your ButtonField.

    Something like:

    VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH | VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR);
    ///
    ///
    HorizontalFieldManager But_Back = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
    ButtonField  Back = new ButtonField("Back",DrawStyle.HCENTER);
    Back.setChangeListener(this);
    But_Back.add(Back);
    vfm.add(But_Back);
    add(vfm);
    

    Concerning

    Bika

  • Problem with setBorder in a buttonField custom

    Hello world

    I have a custombuttonfield and I FOCUS put border for NORMAL, ACTIVE, and DISABLED using:

    field.setBorder (ButtonField.VISUAL_STATE_NORMAL, BorderFactory.createBitmapBorder,...);

    The thing is that there is a scenario in which the button will not change until its border of development, he stays with the same active border when it is on focus. If I add just a button on the screen, it works fine, but if I add another component the focus state does not work. Is it something related to the method of paint used in the component that was added previously? The two components (button and customlabelField) are added to the manager (verticalfieldmanager) of the delegate.

    Any help would be appreciated

    Thanks and greetings

    Can you post an example that demonstrates this behavior, here or in The Issue Tracker?

    What model of Smartphone BlackBerry and the BlackBerry terminal software version you test on?  You can find this under Options, all on the BlackBerry Smartphone.

  • Substitution of CustomFields and getPreferredHeight()


    Hello again, man.

    I tried with a custom button field and my result was constant: 29 before and after the screen got active.

    Probably, you might have a bad layout code or something. Consider this as an example (in fact, it's the code work for me) and understand what you're missing.

    The custom field with the methods:

    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Graphics;
    
    public class CustomButtonField extends Field {
    
        private static final int Y_PADDING = 8;
        private static final int X_PADDING = 8;
    
        private String label;
        private int backgroundColor;
        private int foregroundColor;
        private int focusedForegroundColor;
        private int focusedBackgroundColor;
    
        public CustomButtonField(String label, long style) {
            super(style);
            this.label = label;
            this.backgroundColor = Color.LIGHTGRAY;
            this.foregroundColor = Color.BLACK;
            this.focusedForegroundColor = Color.WHITE;
            this.focusedBackgroundColor = Color.BLACK;
        }
    
        protected void layout(int width, int height) {
            setExtent(getPreferredWidth(), getPreferredHeight());
        }
    
        protected void paint(Graphics graphics) {
            int bgColor = 0;
            int fgColor = 0;
            if (isFocus()) {
                bgColor = focusedBackgroundColor;
                fgColor = focusedForegroundColor;
            }
            else {
                bgColor = backgroundColor;
                fgColor = foregroundColor;
            }
            graphics.setColor(bgColor);
            graphics.fillRoundRect(1, 1, getPreferredWidth() - 2, getPreferredHeight() - 2, 12, 12);
            graphics.setColor(Color.WHITE);
            graphics.setGlobalAlpha(100);
            graphics.fillRoundRect(3, 3, getPreferredWidth() - 6, getPreferredHeight() / 2, 12, 12);
            graphics.setGlobalAlpha(255);
            graphics.setColor(fgColor);
            graphics.drawText(label, X_PADDING / 2, Y_PADDING / 2);
        }
    
        public int getPreferredHeight() {
            return getFont().getHeight() + Y_PADDING;
        }
    
        public int getPreferredWidth() {
            return getFont().getAdvance(label) + X_PADDING;
        }
    
              //The other methods you have to implement to manage focus and key listening go here
    }
    

    I used the code in my previous post to set a Manager on these buttons, but instances of the CustomButtonField instead of the ButtonField class.

    Check it out and let me know if it works.

  • FixedWidth buttonField cannot Center the text


    Hi macdan,.

    I think that in such cases it is advisable to extend the scope rather than ButtonField.

    Try this CustomButtonField. Hope that it will achieve your problem.

    import net.rim.device.api.ui.Field;
    import net.rim.device.api.system.Display;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.XYRect;
    import net.rim.device.api.ui.XYPoint;
    import net.rim.device.api.system.Characters;
    
    public class CustomButtonField extends Field
    {
        private String label;
        private int labelLength;
        private int width;
        private int height;
        private int alignment;
        private XYPoint labelTopLeftPoint;
        private boolean isFocusable;
    
        /**
         * Margin for the button
         */
        private final static int DEFAULT_LEFT_MARGIN = 1;
        private final static int DEFAULT_RIGHT_MARGIN = 1;
        private final static int DEFAULT_TOP_MARGIN = 4;
        private final static int DEFAULT_BOTTOM_MARGIN = 4;
    
        /**
         * Padding for the button
         */
        private final static int DEFAULT_LEFT_PADDING = 5;
        private final static int DEFAULT_RIGHT_PADDING = 5;
        private final static int DEFAULT_TOP_PADDING = 4;
        private final static int DEFAULT_BOTTOM_PADDING = 4;
    
        /**
         * Margins around the text box
         */
        private int leftMargin = DEFAULT_LEFT_MARGIN;
        private int rightMargin = DEFAULT_RIGHT_MARGIN;
        private int topMargin = DEFAULT_TOP_MARGIN;
        private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    
        /**
         * Padding around the text box
         */
        private int leftPadding = DEFAULT_LEFT_PADDING;
        private int rightPadding = DEFAULT_RIGHT_PADDING;
        private int topPadding = DEFAULT_TOP_PADDING;
        private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    
        /**
         * Alignment
         */
         public final static int ALIGNMENT_LEFT = 0x00000001;
         public final static int ALIGNMENT_RIGHT = 0x00000002;
         public final static int ALIGNMENT_TOP = 0x00000004;
         public final static int ALIGNMENT_BOTTOM = 0x00000008;
         public final static int ALIGNMENT_CENTER = 0x00000010;
    
        public final static int DEFAULT_BACKGROUND_COLOR_NORMAL = 0x00ffffff;
        public final static int DEFAULT_BACKGROUND_COLOR_ON_FOCUS = 0x009c0000;
        private int backgroundColorNormal = DEFAULT_BACKGROUND_COLOR_NORMAL;
        private int backgroundColorOnFocus = DEFAULT_BACKGROUND_COLOR_ON_FOCUS;
    
       public final static int ALIGNMENT_DEFAULT = ALIGNMENT_LEFT | ALIGNMENT_TOP;
    
        public CustomButtonField(final String label)
        {
            this(label, 0);
        }
    
        public CustomButtonField(final String label, int width)
        {
            super();
    
            this.label = (label == null) ? "" : label;       
    
            this.isFocusable = true;
            Font font = getFont();
    
            labelLength = font.getAdvance(this.label);
    
            this.width = (width != 0) ? width : (labelLength + leftMargin + leftPadding + rightPadding + rightMargin);
            this.height = font.getHeight() + topMargin + topPadding + bottomPadding + bottomMargin;
    
            labelTopLeftPoint = new XYPoint();
    
            setAlignment(ALIGNMENT_DEFAULT);
        }
    
        public void setWidth(int width)
        {
            int displayWidth = Display.getWidth();
    
            if (width > 0 && width <= displayWidth)
            {
                this.width = width;
                adjustAlignment();
            }
        }    
    
        public void setWidth(String refStr)
        {
            this.labelLength = getFont().getAdvance(refStr);
            int tempWidth = leftMargin + leftPadding +  labelLength + rightPadding + rightMargin;
    
            setWidth(tempWidth);
        }
    
        public void setHeight(int height)
        {
            this.height = height;
        }
    
        public void setSize(int width, int height)
        {
            setWidth(width);
            setHeight(height);
        }    
    
        public void setAlignment(int alignment)
        {
            if ((alignment & ALIGNMENT_CENTER) != 0)
            {
                this.alignment = alignment;
            }
            else
            {
                this.alignment = 0;
    
                if ((alignment & ALIGNMENT_RIGHT) != 0)
                {
                    this.alignment |= ALIGNMENT_RIGHT;
                }
                else
                {
                    this.alignment |= ALIGNMENT_LEFT;
                }
    
                // Vertical alignment
                if ((alignment & ALIGNMENT_BOTTOM) != 0)
                {
                    this.alignment |= ALIGNMENT_BOTTOM;
                }
                else
                {
                    this.alignment |= ALIGNMENT_TOP;
                }
            }
            adjustAlignment();
        }
    
        private void adjustAlignment()
        {
            int leftBlankSpace = leftMargin + leftPadding;
            int rightBlankSpace = rightPadding + rightMargin;
    
            int topBlankSpace = topMargin + topPadding;
            int bottomBlankSpace = bottomMargin + bottomPadding;
    
            if ((alignment & ALIGNMENT_CENTER) != 0)
            {
                int emptySpace = width - (leftBlankSpace + labelLength + rightBlankSpace);
    
                labelTopLeftPoint.y = topBlankSpace;
                labelTopLeftPoint.x = leftBlankSpace + emptySpace/2;
            }
            else
            {
                // Horizontal alignment
                if ((alignment & ALIGNMENT_LEFT) != 0)
                {
                    labelTopLeftPoint.x = leftBlankSpace;
                }
                else if ((alignment & ALIGNMENT_RIGHT) != 0)
                {
                    labelTopLeftPoint.x = width - (labelLength + rightBlankSpace);
                }
                labelTopLeftPoint.y = topBlankSpace;
            }
        }
    
        public String getText()
        {
            return label;
        }
    
        public int getButtonWidth()
        {
            return width;
        }
    
        public void setLeftMargin(int leftMargin)
        {
            if (leftMargin >= 0)
            {
                this.width -= this.leftMargin;
                this.leftMargin = leftMargin;
                this.width += this.leftMargin;
    
                adjustAlignment();
            }
        }
    
        public void setRightMargin(int rightMargin)
        {
            if (rightMargin >= 0)
            {
                this.width -= this.rightMargin;
                this.rightMargin = rightMargin;
                this.width += this.rightMargin;
    
                adjustAlignment();
            }
        }
    
        public void setTopMargin(int topMargin)
        {
            if (topMargin >= 0)
            {
                this.height -= this.topMargin;
                this.topMargin = topMargin;
                this.height += this.topMargin;
                adjustAlignment();
            }
        }
    
        public void setBottomMargin(int bottomMargin)
        {
            if (bottomMargin >= 0)
            {
                this.height -= this.bottomMargin;
                this.bottomMargin = bottomMargin;
                this.height -= this.bottomMargin;
    
                adjustAlignment();
            }
        }
    
        public void setMargin(int topMargin, int rightMargin, int bottomMargin,int leftMargin)
        {
            setLeftMargin(leftMargin);
            setRightMargin(rightMargin);
            setTopMargin(topMargin);
            setBottomMargin(bottomMargin);
        }
    
        public void setFocusable(boolean isFocusable)
        {
            this.isFocusable = isFocusable;
        }
    
        public int getPreferredWidth()
        {
            return width;
        }
    
        public int getPreferredHeight()
        {
            return height;
        }
    
        protected void layout(int width, int height)
        {
            setExtent(Math.min(getPreferredWidth(), width), Math.min(getPreferredHeight(), height));
        }
    
        protected void paint(Graphics graphics)
        {
            int w = width - (leftMargin + rightMargin);
            int h = height - (topMargin + bottomMargin);        
    
            if(isFocus() == false)
            {
                graphics.setColor(backgroundColorNormal);
                graphics.fillRoundRect(leftMargin, topMargin, w, h, 6, 6);
                graphics.setColor(0x00394142);
                graphics.drawRoundRect(leftMargin, topMargin, w, h, 6, 6);
                graphics.drawText(label,  labelTopLeftPoint.x, labelTopLeftPoint.y);
            }
            else
            {
                graphics.setColor(backgroundColorOnFocus);
                graphics.fillRoundRect(leftMargin, topMargin, w, h, 6, 6);
                graphics.drawRoundRect(leftMargin, topMargin, w, h, 6, 6);
    
                graphics.setColor(0x00ffffff);
                graphics.drawText(label,  labelTopLeftPoint.x, labelTopLeftPoint.y);
            }
        }
    
        public boolean isFocusable()
        {
            return isFocusable;
        }
    
        public void getFocusRect(XYRect rect)
        {
            rect.set(leftMargin, topMargin, width - (leftMargin + rightMargin), height - (topMargin + bottomMargin));
        }
    
        protected void drawFocus(Graphics graphics, boolean on)
        {
            invalidate();
        }
    
        public boolean keyChar(char key, int status, int time)
        {
            if (key == Characters.ENTER)
            {
                fieldChangeNotify(0);
                return true;
            }
    
            return false;
        }
    
        protected boolean navigationClick(int status, int time)
        {
            fieldChangeNotify(0);
            return true;
        }
    }
    

    And from your screen main alignment of the button text.

    //CustomButtonField button1 = new CustomButtonField("Button1");
    CustomButtonField button1 = new CustomButtonField("Button1", Display.getWidth() / 2);
    button1.setAlignment(CustomButtonField.ALIGNMENT_CENTER);
    

    Concerning

    Bika

  • Problem with a Bitmap image in a ButtonField (get a 104 NullPointerException error)

    Hi, I'm a beginner in programming BlackBerry Apps, well...

    I tried to create a simple ButtonField customized with a bitmap inside, but I constantly get an error 104 eception: NullPointerException, here is my code to field custom buttond.

    import net.rim.device.api.ui.component.ButtonField;import net.rim.device.api.ui.Graphics;import net.rim.device.api.system.Bitmap;
    
    public class CustomButtonField extends ButtonField{  
    
      private Bitmap imagen;
    
      CustomButtonField(Bitmap imagen, long style){
    
          super(style);     this.imagen = imagen;                     }
    
      public int getPreferredWidth(){
    
                    return 60;        }
    
      public int getPreferredHeight(){
    
          return 60;        }
    
     protected void paint(Graphics graphics) {
    
          graphics.drawBitmap(0, 0, imagen.getWidth(), imagen.getHeight(), imagen, 0, 0);   }
    
       protected void layout(int width, int height) {
    
          setExtent(getPreferredWidth(), getPreferredHeight());
    
        }  }
    
    import net.rim.device.api.ui.container.MainScreen;import net.rim.device.api.system.Bitmap;
    
    public class PruebaScreen extends MainScreen{    
    
      private CustomButtonField boton;
    
      PruebaScreen(){
    
          Bitmap imagen = Bitmap.getBitmapResource("res/img/icon.png");     boton = new CustomButtonField(imagen, 0);     add(boton);
    
      }}
    

    I suspect that my problem is due to the method of painting or the page layout, I don't know... I hope someone can help me, thank you very much in advance.

    Check if your getBitmapResource returns any non-null Bitmap image.  Specify the path correctly may take a few tries ("' / res/img/icon.png '," img/icon.png", etc.")

  • Custom button field has two tops?

    I am trying to create a field of custom button so that I can have two lines of text - but I'm getting a second top so say (a second box with the shading/etc that has the top of a field of button).

    Here's a screenshot of it

    The bottom button is just a normal button.

    Field of custom button code:

    package com.smartmech.bb;
    
    import net.rim.device.api.system.Display;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Ui;
    import net.rim.device.api.ui.component.ButtonField;
    
    public class _customButtonField extends ButtonField {
            private int setWidth;
            private int setHeight;
            private boolean disabled = false;
            private String lblTxt1;
            private String lblTxt2 = "";
    
            _customButtonField( String text, int Width, int Height, long setStyle) {
                super(text, setStyle);
                setWidth = Width;
                setHeight = Height;
                lblTxt1 = text;
            } //customButtonField
    
            _customButtonField( String text, String text2, int Width, int Height, long setStyle) {
                super("",setStyle);
                setWidth = Width;
                setHeight = Height;
                lblTxt1 = text;
                lblTxt2 = text2;
            } //customButtonField
    
            public int getPreferredWidth() {
                return setWidth;
            } //getPreferredWidth
    
            public int getPreferredHeight() {
                return setHeight;
            } //getPreferredWidth
    
            public boolean isFocusable() {
                if(disabled)
                    return false;
                return true;
            } //isFocusable
    
            public void disable() {
                disabled=true;
                super.setVisualState(ButtonField.VISUAL_STATE_DISABLED);
            } //disable
    
            public void enable() {
                disabled=false;
                super.setVisualState(ButtonField.VISUAL_STATE_NORMAL);
            } //enable
    
            protected void layout(int width, int height) {  
    
                String platform = net.rim.device.api.system.DeviceInfo.getPlatformVersion();
    
                if (platform.substring(0,1).equals("5")) {
                    setExtent(setWidth, setHeight); //works in v5
                }
                else if (platform.substring(0,1).equals("6")) {
                    super.layout(setWidth, setHeight);
                    //setExtent(setWidth, setHeight);
                }
    
            } //layout
    
            protected void paint(Graphics g){
    
                super.paint(g);
    
                if (lblTxt2 != "") {
    
                    int myY = getPreferredHeight() / 2;
                    int myX = 0;
    
                    int myFH = g.getFont().getHeight();
                    if (myFH * 2 > getPreferredHeight()) {
                        myY = getPreferredHeight() - myFH;
                    }
                    myX = (getPreferredWidth() - g.getFont().getBounds(lblTxt1)) / 2;
                    g.drawText(lblTxt1, myX,0,0,this.getWidth());
    
                    myX = (getPreferredWidth() - g.getFont().getBounds(lblTxt2)) / 2;
                    g.drawText(lblTxt2, myX,myY,0,this.getWidth());
    
                } //lblTxt2 not blank
    
            } //paint
    
    } //customButtonField
    

    And call it:

    _customButtonField summaryBtn = new _customButtonField("Location", "Summary", btnWidth, btnHeight, ButtonField.CONSUME_CLICK);
     summaryBtn.setFont(mainFont);
    

    Any ideas as to why this is happening?

    Greetings.

    I agree with simon. And that's because you're going to have to completely override the paint method to reach your goal.

    Try stretching the field and drawing and layout of all within your class.

Maybe you are looking for