02-25-2009 10:02 PM
Hi, can someone please help me with HorizontalFieldManage?
I want to display three fields using HorizontalFieldManager, but I can't make it work. Running the following code only leftField and middleField are displayed, middleField is at wrong location. The rightField is not shown at all.
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.HorizontalFieldMan
public class ThreeFieldManager extends HorizontalFieldManager {
private ButtonField leftField;
private EditField middleField;
private ButtonField rightField;
public ThreeFieldManager() {
super(USE_ALL_WIDTH);
leftField = new ButtonField("Left");
middleField = new EditField("Test: ", "ttt");
rightField = new ButtonField("Right");
add(leftField);
add(middleField);
add(rightField);
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setPositionChild(leftField, 0, 0);
setPositionChild(middleField, (getWidth() - middleField.getWidth())/2 , 0);
setPositionChild(rightField, getWidth() - rightField.getWidth() , 0);
}
public int getPreferredWidth() {
return Display.getWidth();
}
}
02-25-2009 10:29 PM
02-26-2009 01:34 AM
Add all your three fields in horizontal field manager first then add field manager on screen instead of directly adding 3 fields directly on screen.
HorizontalFieldManager hf = null; hf = new HorizontalFieldManager(); hf.add(leftField); hf.add(middleField); hf.add(rightField); add(hf);
02-26-2009 01:40 AM
Thanks. Tried a button as middle field the code worked.
Is there a way to change EditField's size?
02-26-2009 02:53 AM
you can use this link
http://na.blackberry.com/eng/devjournals/resources
PS: If you need to thank, give Kudos
02-26-2009 08:34 AM
Deepesh wrote:Add all your three fields in horizontal field manager first then add field manager on screen instead of directly adding 3 fields directly on screen.
HorizontalFieldManager hf = null;hf = new HorizontalFieldManager(); hf.add(leftField);hf.add(middleField);hf.add(right
Field);add(hf);
That's no different from what he initially posted.
02-26-2009 08:39 AM
Hi,
I guess following code will help you in solving your problem.
class TreeComponentScreen extends FullScreen{
EditField objEditField = new EditField("Color:"," Red",10,Field.FOCUSABLE);
LabelField objLabel = new LabelField("<Label1>");
LabelField objLabel1 = new LabelField("<Label2>");
TreeComponentScreen() {
drawComponent();
}
public void drawComponent(){
VerticalFieldManager hfm = new VerticalFieldManager();
hfm.add(objLabel);
hfm.add(objEditField);
hfm.add(objLabel1);
add(hfm);
}
}
If you have any query please let me know.
Regards,
Rajat Gupta.
02-26-2009 08:45 AM - edited 02-26-2009 10:40 AM
You seem to getting a number of redundant/duplicate posts on this problem. for example Rajat_10Sep's post doesn't seem to differ from anything else that has been suggested!
I think the issue here is you need to restrict the width of the EditField. I think you can do this by overriding the getPreferredWidth() and setting just the width you need, plus overriding layout() so it uses your preferred width.
Edit: Now had time to create some sample code. Hope this helps:
HorizontalFieldManager hfm2 = new HorizontalFieldManager(HorizontalFieldManager.USE_
ALL_WIDTH); hfm2.add(new ButtonField("Left")); EditField test = new EditField("Test: ", "ttt") { public int getPreferredWidth() { return this.getFont().getAdvance(this.getLabel() + this.getText() + " "); } public void layout(int width, int height) { super.layout(getPreferredWidth(), getPreferredHeight()); setExtent(getPreferredWidth(), getPreferredHeight()); } }; hfm2.add(test); hfm2.add(new ButtonField("Right")); this.add(hfm2);
Note that I think this code will not work very well when the EditField is updated by the user. Not tested that, just tried to get the Fields displayed as you wanted.
02-26-2009 01:39 PM
Thanks for all your help! By reading all the replies I solved it by wrapping the fields in VerticalFieldManager before adding to HoriontalFieldManager.
Don't know why I cannot change size of EditField by implementing its getPreferedWidth() and Layout() methods.