1

I've been struggling to create multiple VerticalFieldManager dynamically on Blackberry. Each subManager will show different data to the user. And each subManager will have a different position on the screen. So i created a class which has a "mainManager" and another class which creates the "submanagers" then i call mainManager.add(new TheClassExtendingVerticalFieldManager); to add the subManagers to the mainManager. The problem is that i only get one subManager instead of three. I'm using padding to separate the managers. Here's the code im using. Please guide me in the right direction, it would be much appreciated.

The class that creates the submanager

public class ProgramListView extends VerticalFieldManager{

    private VerticalFieldManager subManager;
    private int _height;


    public ProgramListView(int height){
        this._height = height;

//      subManager = new VerticalFieldManager(
//              Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR | 
//              Manager.NO_HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH)
//
//      {
//
//
//      };
    }

    public int get_height() {
        return _height;
    }

    public void set_height(int _height) {
        this._height = _height;
    }

    public void setCoordinates(int x, int y){
        setPosition(100,140);
    }
    protected void sublayout(int maxWidth, int maxHeight)
    {
        int displayWidth = Display.getWidth();
        int displayHeight = maxHeight;
        this.setPosition(300, 300);
        super.sublayout( 40, 40);
        setPadding(this.get_height(), 0, 0, 0);
        setExtent(displayWidth, this.get_height());

    }

    public void paint(Graphics graphics)
    {
        graphics.setBackgroundColor(Color.BLUE);//blue
        graphics.clear();
        super.paint(graphics);      
    }
    public int getPreferredWidth() {
        // TODO Auto-generated method stub
        return Display.getWidth();
    }

} 

The mainManager class

public class ProgramLayout extends MainScreen {

    private HorizontalFieldManager mainManager;
    private int deviceWidth = Display.getWidth();
    private int deviceHeight = Display.getHeight();
    private Vector subManagers;
    private int theheight;

    public ProgramLayout(){

        setToolbar();
        subManagers = new Vector();
        theheight = 100;
        mainManager = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT)

        {

            protected void sublayout(int maxWidth, int maxHeight)
            {
                int displayWidth = deviceWidth;
                int displayHeight = deviceHeight;
                super.sublayout( displayWidth, displayHeight);
                setExtent(displayWidth, displayHeight);
            }

            public void paint(Graphics graphics)
            {
                graphics.setBackgroundColor(Color.BLACK);
                graphics.clear();
                super.paint(graphics);
            }

            public int getPreferredWidth() {
                // TODO Auto-generated method stub
                return Display.getWidth();
            }

        };
        for (int i = 0; i < 3; i++) {
            theheight = theheight+100;
            subManagers.addElement(new ProgramListView(theheight));
        }

        for (int i = 0; i < subManagers.size(); i++) {

            mainManager.add((VerticalFieldManager)subManagers.elementAt(i));
        }


        this.add(mainManager);

    }

Thanks in advance

madcoderz
  • 4,423
  • 10
  • 47
  • 74
  • 2
    Maybe because you're using HorizontalFieldManager and sub manager class is programmed to take all screen width – Eugen Martynov Jul 13 '12 at 18:38
  • 1
    Just to elaborate on what Eugen pointed out, your `mainManager` is a `HorizontalFieldManager`, which means it will lay out child fields in the order that you `add()` them, horizontally. But, each child is an instance of `ProgramListView` and returns `Display.getWidth()` in `getPreferredWidth()`. So, the first one takes up the whole screen width. Did you want these three `ProgramListView`s to be stacked **vertically**? Then, `mainManager` should be a `VerticalFieldManager`. – Nate Jul 14 '12 at 06:52
  • Thanks guys. Changing the main manager to vertical did the trick. – madcoderz Jul 14 '12 at 09:36
  • Hey @Nate if you put your comment as an actual answer i could select it as the correct answer. Thanks – madcoderz Jul 17 '12 at 19:34
  • Comment posted as answer as requested :) – Nate Jul 22 '12 at 07:53

2 Answers2

0

try this:

    for (int i = 0; i < 3; i++) {
        theheight = theheight+100;
         mainManager.add(new ProgramListView(theheight));
    }
    this.add(mainManager);
payal
  • 162
  • 7
0

Just to elaborate on what Eugen Martynov pointed out, your mainManager is a HorizontalFieldManager, which means it will lay out child fields in the order that you add() them, horizontally. It automatically handles layout for you, based on the order of calls you make to add().

But, each child is an instance of ProgramListView and returns Display.getWidth() in getPreferredWidth():

public int getPreferredWidth() {         
    // TODO Auto-generated method stub         
    return Display.getWidth();     
}

So, the first one takes up the whole screen width, and the next two would have to be to the right of that (but you've already taken up the whole screen width).

Did you want these three ProgramListViews to be stacked vertically? Then, mainManager should just be changed to a VerticalFieldManager. With a VerticalFieldManager, calling add() for three different ProgramListView child fields will automatically lay them out vertically.

Nate
  • 31,017
  • 13
  • 83
  • 207