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
Developer
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified

how to draw a border around an editfield,objectchoicefied etc

hi,

i am using editfied ,ObjectChoiceField in my application , everything is right but doesn't look good.

You know i mean , i want to draw border around the editfield , how can i do that.

any suggestions!

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: how to draw a border around an editfield,objectchoicefied etc

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to draw a border around an editfield,objectchoicefied etc

Also overriding paint as follows might work for you too:

 

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        int stipple = graphics.getStipple(); // save current Stipple
        graphics.setStipple(0xFFFFFFFF);
        graphics.drawRect(0,0, this.getWidth(), this.getHeight()-1);
        graphics.setStipple(stipple);
    }

Developer
Posts: 173
Registered: ‎06-11-2009
My Device: Not Specified

Re: how to draw a border around an editfield,objectchoicefied etc

I think it should be mentioned that starting 4.6 there are two classes: Border and BorderFactory are available.
Developer
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified

Re: how to draw a border around an editfield,objectchoicefied etc

How can i use Border class to change or set the border of an

EditField.

Any suggestions !

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Highlighted
Developer
Posts: 43
Registered: ‎09-28-2009
My Device: Not Specified

Re: how to draw a border around an editfield,objectchoicefied etc

Hi jackofall,

 

I actually wanted to start a Thread on how to draw appropriate borders, but as you are enquiring I might as well provide this info here.

 

There are two ways you can look at depending on what OS models you want to support. If you wish to support 4.6 OS and up, only then you can use the Border classes provided within the 4.6 and up OS releases as follows,

 

Lets say you want a nice rounded Border around the object then we can:

//create border
Border roundedBorder = BorderFactory.createRoundedBorder(new net.rim.device.api.ui.XYEdges(5,5,5,5));
//add border to object
objectToPlaceBorderAround.setBorder(roundedBorder);

 

But now, lets say you want to support OS version 4.2 and up, now you are out of luck because Blackberry only added Borders in OS 4.6, however we still have a solution, it just requires a bit more work. To get this working I prefer to use a Manager to encapsulate my Border, you can use a normal Manager, VerticalFieldManager, HorizontalFieldManager, just as you prefer...For my example I will use a VerticalFieldManager;

 

So Drawing the Border on the VerticalFieldManager is easy enough all we need to do is override paint and draw our rounded border as follows

 

 

public void paint(Graphics graphics) {
super.paint(graphics);
//add your border color you would like
graphics.setColor(Color.WHITE);
//draw the border size and rounded edges you would like.
graphics.drawRoundRect(0, 0, getPreferredWidth(), getPreferredHeight(), 10, 10);
}

 

Now whatever object we add to the VerticalBorderFieldManager will have a nice white rounded edge border around it, however because the normal layout manager is still taking control of actually laying out the fields on the Manager we have the containedField being drawn through the Border created. So we need to handle the layout out of the objects in the Border ourselves as follows;

 

 

public void sublayout(int iWidth, int iHeight) { //call sublayout to set the initial extent we will be working //with, we will redefine this later but it gives us our canvas //Height can be specified as anything as it does not matter super.sublayout(iWidth, Integer.MAX_VALUE); //we collect all the fields in the manager int iNumFields = getFieldCount(); int iYPos = 5; //these values will be used for padding, so 5px int iXPos = 5; //these values will be used for padding, so 5px Field fField = null; for (int i = 0; i < iNumFields; i++) { fField = this.getField(i); //set field position setPositionChild(fField, iXPos, iYPos); //get height for layout and height int tmpHeight = getHeight(fField); //draw field layoutChild(fField, getWidth(fField), tmpHeight); iYPos = iYPos + tmpHeight+5; } //now we reset our extent to include padding this.setExtent(getPreferredWidth(), Math.max(iYPos, getPreferredHeight())); setVirtualExtent(getPreferredWidth(), Math.max(iYPos, getPreferredHeight())); }

 

 The only thing remaining is to fix the Height values we receive, so lastly we need to set our own height returned;

 

public int getPreferredHeight() { height = 5; int iNumFields = getFieldCount(); for (int i = 0; i < iNumFields; i++) { height += getHeight(getField(i))+5; } return height; } private int getHeight(Field f) { return Math.max(Math.max(f.getContentHeight(), f.getHeight()),f.getPreferredHeight()); }

 

 

 

 

 And know you have your very own Border class that can give a border with nice padding to any UI Application running any OS.

 

Hope this helps.

 

Regards

 

Andre