Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
missmanar1987
Posts: 32
Registered: ‎06-26-2010
My Device: all
My Carrier: stc

Re: Right-aligning Text in (Basic)EditField

really I need help why there is no answer !!

Please use plain text.
New Contributor
jingshan
Posts: 2
Registered: ‎12-03-2009
My Device: Not Specified

Re: Right-aligning Text in (Basic)EditField

I hope the following code may help, I wrote this to implement a number-input box, it's fixed size and text-right-aligned editfield, I didn't handle the cursor for the right-aligned text and I just block that function, the code is compiled using JDE 4.5.

 

class MyNumInputField extends EditField
{
    private int myWidth;
    private int myHeight;
    private int myMaxChars;
    private boolean isDrawFocus;
    private String text = "";
    public MyNumInputField(int width, int height, int maxChars)
    {
        super(JUMP_FOCUS_AT_END | NO_LEARNING | NO_NEWLINE);
        myWidth = width;
        myHeight = height;
        myMaxChars = maxChars;
        isDrawFocus = false;
    }
    protected void layout(int width,int height)
    {
        super.layout(myWidth+2, myHeight+2);
        setExtent(myWidth+2, myHeight+2);
    }
    protected void paint(Graphics graphics)
    {
        graphics.drawText(text, 2, 2, DrawStyle.RIGHT, myWidth);
        graphics.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
        if (isDrawFocus)
        {
            graphics.setColor(0x000000ff);
            graphics.drawRect(0, 0, getWidth(), getHeight());
            graphics.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
        }
    }
    protected void onFocus(int direction)
    {
        isDrawFocus = true;
        super.onFocus(direction);
        invalidate(0, 0, getWidth(), getHeight());
    }
    protected void onUnfocus()
    {
        isDrawFocus = false;
        super.onUnfocus();
        invalidate(0, 0, getWidth(), getHeight());
    }
    protected void drawFocus(Graphics graphics, boolean on){}
    protected boolean keyChar(char key, int status, int time)
    {
        String strKey;
        switch (key)
        {
            case Characters.LATIN_CAPITAL_LETTER_W:
            case Characters.LATIN_SMALL_LETTER_W:
            case Characters.DIGIT_ONE:
            {
                strKey = "1";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_E:
            case Characters.LATIN_SMALL_LETTER_E:
            case Characters.DIGIT_TWO:
            {
                strKey = "2";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_R:
            case Characters.LATIN_SMALL_LETTER_R:
            case Characters.DIGIT_THREE:
            {
                strKey = "3";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_S:
            case Characters.LATIN_SMALL_LETTER_S:
            case Characters.DIGIT_FOUR:
            {
                strKey = "4";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_D:
            case Characters.LATIN_SMALL_LETTER_D:
            case Characters.DIGIT_FIVE:
            {
                strKey = "5";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_F:
            case Characters.LATIN_SMALL_LETTER_F:
            case Characters.DIGIT_SIX:
            {
                strKey = "6";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_Z:
            case Characters.LATIN_SMALL_LETTER_Z:
            case Characters.DIGIT_SEVEN:
            {
                strKey = "7";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_X:
            case Characters.LATIN_SMALL_LETTER_X:
            case Characters.DIGIT_EIGHT:
            {
                strKey = "8";
                break;
            }
            case Characters.LATIN_CAPITAL_LETTER_C:
            case Characters.LATIN_SMALL_LETTER_C:
            case Characters.DIGIT_NINE:
            {
                strKey = "9";
                break;
            }
            case Characters.DIGIT_ZERO:
            {
                strKey = "0";
                break;
            }
            case Characters.BACKSPACE:
            {
                text = "";
                invalidate(0, 0, getWidth(), getHeight());
                return true;
            }
            default:
            {
                return super.keyChar(key, status, time);
            }
        }
        if (getTextLength() < myMaxChars)
        {
            text = text + strKey;
            invalidate(0, 0, getWidth(), getHeight());
        }
        return true;
    }
}

 

 

 

Please use plain text.
Developer
n8r0n
Posts: 40
Registered: ‎03-29-2009
My Device: Storm 9550
My Carrier: AT&T

Re: Right-aligning Text in (Basic)EditField

Without running this code, I can't be sure.  But, I have a feeling that Deepesh's problem was that he wasn't calling the base class's method in his onFocus() and onUnfocus().  That's really a completely separate problem, regarding overriding onFocus and onUnfocus.  It doesn't have anything to do with right-alignment of the edit field.

 

Jingshan is calling super.onFocus() and super.onUnfocus() in his implementation of onFocus() and onUnfocus().

 

Concerning missmanar1987, I use this exact class, CustomEditField in a Manager where it is not the last field added.  All fields added after this one are working fine for me.  I wonder if something is changing the Graphics context in a method, and not resetting it after it's done?

Please use plain text.
Developer
n8r0n
Posts: 40
Registered: ‎03-29-2009
My Device: Storm 9550
My Carrier: AT&T

Re: Right-aligning Text in (Basic)EditField

Looking at my original implementation

 

 

      // reset the graphics state to where it was
      g.setColor(oldColor);
      g.setGlobalAlpha(oldAlpha);

 

At the end of my implementation of paint(Graphics g), I undo the changes I made to the color and global alpha properties.  I think that should be safe, in terms of not messing up the Graphics object after the CustomEditField is done painting.

 

Please use plain text.
Contributor
bryanallott
Posts: 10
Registered: ‎10-23-2009
My Device: Not Specified

Re: Right-aligning Text in (Basic)EditField

stumbled upon this ... ?

 

http://stackoverflow.com/questions/2007975/basiceditfield-customization

 

Tried it ... almost there...

Please use plain text.
Contributor
famu1hundred
Posts: 20
Registered: ‎02-16-2010
My Device: 9800
My Carrier: AT&T

Re: Right-aligning Text in (Basic)EditField

I adjusted the original code a little and came up with the following for my EditFields to RIGHT_ALIGN. This fixes the issue with elements NOT SHOWING up after this one as well

 

class CustomEditField extends Manager {
  private int _fieldHeight = 0;
  private int _fieldWidth = 0;
  private long _alignment;
  private Font _font = Font.getDefault();
  private AlignableEditField _editField;

  public CustomEditField(String initialValue, int maxNumChars, long style) {
   super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR);
   _alignment = (style & BasicEditField.FIELD_HALIGN_MASK);

   _editField = new AlignableEditField(null, initialValue,
     maxNumChars, style);
   add(_editField);
  }

  protected void paint(Graphics g) {
   super.paint(g);
  }

  public int getPreferredWidth() {
   return _fieldWidth;
  }

  public int getPreferredHeight() {
   return _font.getHeight();
  }

  public String getText() {
   return _editField.getText();
  }

  public void setFont(Font value) {
   _font = value;
   _editField.setFont(_font);
  }

  protected void sublayout(int width, int height) {
   if (_fieldWidth == 0) {
    _fieldHeight = height;
    _fieldWidth = width;
    setExtent(width, _fieldHeight);
   }

   int textWidth = _editField.getPreferredWidth();

   layoutChild(_editField, textWidth, _fieldHeight);

   if (_alignment == FIELD_RIGHT) {
    setPositionChild(_editField, getPreferredWidth() - textWidth, 0);
   } else if (_alignment == FIELD_HCENTER) {
    setPositionChild(_editField,
      (getPreferredWidth() - textWidth) / 2, 0);
   } else { // FIELD_LEFT
    setPositionChild(_editField, 0, 0);
   }
  }

  private void layoutEditField() {
   sublayout(_fieldWidth, _fieldHeight);
  }

  private class AlignableEditField extends BasicEditField {

   public AlignableEditField(String label, String initialValue,
     int maxNumChars, long style) {
    super(label, initialValue, maxNumChars, style);
   }

   public int getPreferredWidth() {
    return Math.max(_font.getAdvance(getText()),
      _font.getAdvance('0'))
      + _font.getAdvance(' ');
   }

   protected boolean keyChar(char key, int status, int time) {
    boolean result = super.keyChar(key, status, time);
    layoutEditField();
    return result;
   }
  }

 }

Please use plain text.
New Developer
sujith_a
Posts: 35
Registered: ‎02-21-2012
My Device: Blackberry 9300
My Carrier: Developer

Re: Right-aligning Text in (Basic)EditField

Hi all,

 

I am using this custom edit field in my application.But when i am using custom editfield with style "FIELD_RIGHT" ,a space is coming  between the text and the right side of the field.How to remove this space?Any help?

 

 

Thanks in advance

Please use plain text.