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
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Problem painting text

[ Edited ]

 

Hi,  I have a custom field for text field. It has a label & an editfield. I want to set the font and color of the label for whiich I got to use drawText in paint. I tried to add empty label, keeping text width's space, layout only the text field keeping room for the text, and erasing/painting the label text and drawing the text again with my font and color. I failed with all these trials. My code is :-

 

 

public class MyTextField extends HorizontalFieldManager {
    public MyTextField(String label, String initialValue, int maxChars, int textEntryArea, long style) {    
        super(NO_HORIZONTAL_SCROLL);
        textWidth = textEntryArea;
        labelText = label;
        textMgr = new HorizontalFieldManager(HORIZONTAL_SCROLL);
        labelFld = new LabelField(labelText);
        add(this.labelFld);
        _editField = new EditField("", initialValue, maxChars, style | EditField.NO_NEWLINE | EditField.FOCUSABLE | EditField.EDITABLE);
        _editField.setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
        border = BorderFactory.createRoundedBorder(new XYEdges(5, 5, 5, 5), Color.WHITE, Border.STYLE_FILLED);
        textMgr.add(_editField);
        add(textMgr);
        setBorder(border);
    }

    protected void sublayout(int width, int height) {
        int h, w;
        Font inputFont = _editField.getFont();
        Font labelFont = labelFld.getFont();
        int labelSize = labelFont.getAdvance(labelFld.getText());
        int inputMaxSize = textWidth * inputFont.getAdvance("W");
        int myMaxWidth = labelSize + inputMaxSize + 2;  // * border;  
        // Tried to just add textMngr, this gives weird results
        /*
        layoutChild(textMgr, width, this.getHeight());
        setPositionChild(textMgr, labelSize+2, 2);
        
        setExtent(Math.min(myMaxWidth, width), height);
        */
        super.sublayout(Math.min(myMaxWidth, width), height);
    }
    
    protected void paint(Graphics g) {
        int oldColor = g.getColor();
        g.setColor(Color.WHITE);
        g.setBackgroundColor(Color.WHITE);
        // Trying to draw empty strings, but no results
        /*g.drawText("         ", 2, 2);
        g.drawText("        ", 4, 2);
        */
        
        // Try to fill that area with white background, so the label text will be erased. 
        // fills a rect of black color
        g.fillRect(1, 1, labelFld.getPreferredWidth()+2, labelFld.getPreferredHeight()+2);

        g.setColor(0x5f6368);
        Font oldFont = g.getFont();
        
        g.setFont(labelFld.getFont());
        g.drawText(labelFld.getText(), 5, 2);
        
        g.setColor(oldColor);
        g.setFont(oldFont);
        super.paint(g);
    }
}

 

Can anyone help me to paint the text of label and add editfield in sublayout. I have spent much time on this small task. Any help is highly appreciated.

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Highlighted
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Problem painting text

Two comments. First, when you call super.paint(), it undoes all the painting you have done. So calling super.paint() as the first thing will work better.

 

Another approach is to not override paint() at all. Instead, why not just use a LabelField to render your label. Your custom manager can set the font and colors for the LabelField instead of trying to render the label directly in paint().




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Problem painting text

It looks as a modified Scrollable one-line text input field with some strange additions to sublayout and paint.  You don't need to override those at all! Since you are using a built-in HorizontalFieldManager, a simple setMargin() on the labelFld can take care of the positioning you are trying to achieve. Graphics.setFont afftects only subsequent drawText and not any super.paint() - it uses the Field's font anyway and ignores the current Graphics settings.

 

The only paint() you need to override is that of labelFld itself, so create it like that:

 

// Put this among your class members
private int _labelFontColor = Color.BLACK;
...
// The following goes somewhere in the constructor
labelFld = new LabelField(labelText) {
  protected void paint(Graphics g) {
    int prevColor = g.getColor();
    g.setColor(_labelFontColor);
    super.paint();
    g.setColor(prevColor);
  }
};
...
// You'll need these two methods too
public void setLabelFontColor(int color) {
  _labelFontColor = color;
  invalidateFieldRange(0, 0);
}

public void setLabelFont(Font font) {
  labelFld.setFont(font);
  updateLayout();
}

 When you use this class you can do it like this:

 

MyTextField inputField = new MyTextField(
  "label", "", maxChars, // whichever maxChars you need
  textEntryArea, Field.EDITABLE); // what is that textEntryArea, anyway?

inputField.setLabelFontColor(Color.GREEN);
inputField.setLabelFont(myBiggerBolderFont);

 

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!