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
New Developer
Posts: 2
Registered: ‎08-04-2008
My Device: Not Specified

How to Add TextField and ButtonField in Custom HorizontalFieldManager

 

Hi,

   I m new in Blackberry JDE. I want to make a screen containing Custom HorizontalFieldManager. The HorizontalFieldManager will contain a TextField object and a buttonField. Please help me by mentioning some simple code.

     I have tried to wrote the code as following but it is not showing neither TextField nor ButtonField.

 

import net.rim.device.api.system.Bitmap; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.ui.*; final class HomeScreen extends MainScreen { Bitmap headerImg; LabelField label; TopFieldManager topField; SearchTextField searchField; CallButtonField button; Bitmap buttonImg; Bitmap topperImg; /** Creates a new instance of HomeScreen */ public HomeScreen() { super(); headerImg = Bitmap.getBitmapResource("header.png"); buttonImg = Bitmap.getBitmapResource("button.png"); topperImg = Bitmap.getBitmapResource("topper.png"); HeaderField header = new HeaderField(""); setTitle(header); this.add(new SeparatorField()); searchField = new SearchTextField(); button = new CallButtonField(); topField = new TopFieldManager(); topField.add(searchField); topField.add(button); topField.repaint(); add(topField); } class HeaderField extends LabelField{ HeaderField(String str){ super(str,Field.USE_ALL_HEIGHT|DrawStyle.ELLIPSIS); this.invalidate(); } protected void paint(Graphics g){ g.setBackgroundColor(0x00AAAAAA); g.drawBitmap(0,0,headerImg.getWidth(),headerImg.getHeight(),headerImg,0,0); g.setColor(0x00FFFFFF); g.drawText("Summery",5,(getHeight()-g.getFont().getHeight())/2); } public int getPreferredWidth() { return headerImg.getWidth(); } public int getPreferredHeight() { return headerImg.getHeight(); } public void repaint(){ invalidate(); } } class CallButtonField extends ButtonField{ CallButtonField(){ super(); this.invalidate(); } protected void paint(Graphics g){ g.drawBitmap(0,0,buttonImg.getWidth(),buttonImg.getHeight(),buttonImg,0,0); g.setColor(0x000000AA); g.drawText("New Call",5,(getHeight()-g.getFont().getHeight())/2); } public int getPreferredWidth() { return buttonImg.getWidth(); } public int getPreferredHeight() { return buttonImg.getHeight(); } public void repaint(){ invalidate(); } } class SearchTextField extends TextField{ Font font; SearchTextField(){ super(); font = Font.getDefault(); invalidate(); } protected void paint(Graphics g){ g.setColor(0x00FFFFFF); g.fillRect(0,0,topperImg.getWidth()/2,font.getHeight()); g.setColor(0x00101010); } public int getPreferredWidth() { return topperImg.getWidth()/2; } public int getPreferredHeight() { return font.getHeight(); } public void repaint(){ invalidate(); } } class TopFieldManager extends HorizontalFieldManager{ TopFieldManager(){ super(Manager.NO_VERTICAL_SCROLL|Manager.NO_HORIZONTAL_SCROLL); invalidate(); } protected void paint(Graphics g){ g.drawBitmap(0,0,topperImg.getWidth(),topperImg.getHeight(),topperImg,0,0); } public int getPreferredWidth() { //return Graphics.getScreenWidth(); return topperImg.getWidth()-100; } public int getPreferredHeight() { return topperImg.getHeight()+50; } protected void sublayout(int width, int height) { int x = 1; int y = 1; Field field; for (int i = 0; i < getFieldCount(); i++) { field= getField(i); setPositionChild(field,x,y); layoutChild(field,width - x,height - y); x += field.getWidth(); } setExtent(width,height); } public void repaint(){ invalidate(); } } }

 

    I have run this code in Jde 4.3 and 8120 simulator. I dont know what is the problem with this coding, Please help me regarding this.

 

 

Thanks and regards

Hitangshu

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

Re: How to Add TextField and ButtonField in Custom HorizontalFieldManager

I suspect you are trying to do a bit much too soon.

 

I would this bit at a time.  Start by getting your various fields working when added to the HomeScreen , then move them to a manager, then make yourManager more complicated, then try basing your manager on an image and lastly try positioning your fields (assuming you can't do this some more typical way). 

 

As an example of where you are going wrong, the paint method in your TopFieldManager only paints an image, so will never display anything except your "topper.png" image.

 

There are a number of other unusual things in this code - including (and in no particular order):

a) After a g.setBackgroundColor() you usually code a g.clear()

b) You call repaint before the field is even added to a screen

c) Both SearchTextField and TopFieldManager seem to be sized on "topper.png"

 

So start again slowly and test everything as you go.

 

Just my 2p worth....

New Developer
Posts: 2
Registered: ‎08-04-2008
My Device: Not Specified

Re: How to Add TextField and ButtonField in Custom HorizontalFieldManager

Hi

 Thanks for reply, I will try to go as u suggest. However for any problem reply as I m new in this environment.

 

Hitangshu