Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font color of blackberry label field dynamically?

I have one label field and three buttons with the name of red, yellow, blue. If I click the red button then the label field font color should be change to red; similarly if I click the yellow button then the font color should change to yellow; likewise according to the button color the color of font should change in the label field.

Can anyone tell me how to do this?

like image 784
Kumar Avatar asked Sep 02 '09 07:09

Kumar


1 Answers

Font color in LabelField is easily maintained by setting graphics.setColor on paint event before super.paint:

    class FCLabelField extends LabelField {
        public FCLabelField(Object text, long style) {
            super(text, style);
        }

        private int mFontColor = -1;

        public void setFontColor(int fontColor) {
            mFontColor = fontColor;
        }

        protected void paint(Graphics graphics) {
            if (-1 != mFontColor)
                graphics.setColor(mFontColor);
            super.paint(graphics);
        }
    }

    class Scr extends MainScreen implements FieldChangeListener {
        FCLabelField mLabel;
        ButtonField mRedButton;
        ButtonField mGreenButton;
        ButtonField mBlueButton;

        public Scr() {
            mLabel = new FCLabelField("COLOR LABEL", 
                    FIELD_HCENTER);
            add(mLabel);
            mRedButton = new ButtonField("RED", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mRedButton.setChangeListener(this);
            add(mRedButton);
            mGreenButton = new ButtonField("GREEN", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mGreenButton.setChangeListener(this);
            add(mGreenButton);
            mBlueButton = new ButtonField("BLUE", 
                    ButtonField.CONSUME_CLICK|FIELD_HCENTER);
            mBlueButton.setChangeListener(this);
            add(mBlueButton);
        }

        public void fieldChanged(Field field, int context) {
            if (field == mRedButton) {
                mLabel.setFontColor(Color.RED);
            } else if (field == mGreenButton) {
                mLabel.setFontColor(Color.GREEN);
            } else if (field == mBlueButton) {
                mLabel.setFontColor(Color.BLUE);
            }
            invalidate();
        }
    }
like image 116
Maksym Gontar Avatar answered Sep 20 '22 05:09

Maksym Gontar