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: 57
Registered: ‎10-29-2008
My Device: Not Specified

HorizontalFieldManager question

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.HorizontalFieldManager;

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();
    }
}

Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: HorizontalFieldManager question

An EditField uses the whole width of the screen, which is why the third element is not being displayed.
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: HorizontalFieldManager question

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);

 

 


If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish

 

 

 

 

 

 

Deepesh Gupta
New Developer
Posts: 57
Registered: ‎10-29-2008
My Device: Not Specified

Re: HorizontalFieldManager question

Thanks. Tried a button as middle field the code worked.

Is there a way to change EditField's size? 

Highlighted
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: HorizontalFieldManager question

you can use this link

 

http://na.blackberry.com/eng/devjournals/resources/journals/jul_2005/creating_textbox_field.jsp

 

 

PS: If you need to thank, give Kudos


If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish

 

 

Deepesh Gupta
Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: HorizontalFieldManager question


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(rightField);add(hf);

 


That's no different from what he initially posted. 

 

Developer
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: HorizontalFieldManager question

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.


If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: HorizontalFieldManager question

[ Edited ]

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.

Message Edited by peter_strange on 02-26-2009 03:40 PM
New Developer
Posts: 57
Registered: ‎10-29-2008
My Device: Not Specified

Re: HorizontalFieldManager question

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.