BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-08-2008, 04:22 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default I'm stuck on a simple grid...

Please Login to Remove!

I have done a lot of reading here and used a lot of examples posted but I still can't get this to work. Note that I'm a newbie!

I want a simple grid on my main screen with 4 rows and 4 columns. The first column should be a little wider than the other 3 since it contains a checkboxfield; the remaining 3 columns contain editfields. Would be nice to separate each column by a space but at this point I don't care if they're all stuck together. My aim is to have the 3 editfields lined up vertically on the right side of the screen. It would look like this:

Header1...Hdr2....Hdr3....Hdr4
chkbox1...edit1a..edit1b..edit1c
chkbox2...edit2a..edit2b..edit2c

My thought-process is to use horizontalfieldmanager and verticalfieldmanager. But when I do this none of the fields line up. They always seem to be placed horizontally one after the other. The checkbox fields on each row are not all the same width so the editbox fields will not align.

I thought that adding 4 vfm's to a hfm would result in evenly spaced columns afterwhich adding the fields would line up. But it seems that the width is determined by the field being added.

Here's a snippet of my code so far:

Code:
    HorizontalFieldManager hfm1 = new HorizontalFieldManager();
    VerticalFieldManager vfm11 = new VerticalFieldManager();
    VerticalFieldManager vfm12 = new VerticalFieldManager();
    VerticalFieldManager vfm13 = new VerticalFieldManager();
    VerticalFieldManager vfm14 = new VerticalFieldManager();
    
    vfm11.add(new CheckboxField("Name", true)); // first column
    vfm12.add(new FixedWidthField("", "text1")); // second column
    vfm13.add(new FixedWidthField("", "text2")); // third column
    vfm14.add(new FixedWidthField("", "text3")); // fourth column
    hfm1.add(vfm11);
    hfm1.add(vfm12);
    hfm1.add(vfm13);
    hfm1.add(vfm14);
    add(hfm1);

//Repeat the above for each additional row


//My custom field; required because I want to limit the width & I plan
//adding borders

  public class FixedWidthField extends EditField
  {
   public FixedWidthField(String c, String d)
    {
      super(c, d);
    }

    public int getPreferredWidth()
    {
      return 100;
    }

    public void layout(int maxWidth, int maxHeight)
    {
      super.layout(getPreferredWidth(), super.getPreferredHeight());
      setExtent(getPreferredWidth(), super.getPreferredHeight());
    }
 }
I suspect I need a custom vfm. Is that the only way to set the columns to a fixed width? Can anyone provide a short example of a custom vertical field manager or point me to one?

Thanks...

Last edited by dynot : 08-08-2008 at 04:24 PM.
Offline  
Old 08-08-2008, 10:15 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Try this. I haven't used it for a while but I used it in a project last year to do what your asking for.

Code:
/*
 * LayoutGrid.java
 *
 * 
 */

package opt.util;

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;

public class LayoutGrid extends Manager
{
    private int cols, layoutWidth, layoutHeight;
    private int[] colsWidth;
    
    
    public LayoutGrid(int nCols)
    {
        //construct a manager with vertical scrolling
        super(0);
        cols = nCols;
        colsWidth = new int[cols];
    }
    
    //overwrite the nextFocus method for custom navigation
    protected int nextFocus(int direction, boolean alt)
    {
        // this doesn't really override.
        return super.nextFocus(direction, alt);
    }
    
    protected void sublayout(int width, int height)
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();
        int x = 0;
        int y = 0;
        int w = 0;
        
        layoutWidth = width;
        layoutHeight = height;
        
        int pw = getPreferredWidth();
        int pad = 0;
        if (width > pw)
            pad = (width - pw) / cols;
        
        for (int j = 0; j < numberOfFields; j += cols)
        {
            int h = 0;
            x = 0;
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                setPositionChild(field,x,y); //set the position for the field
                layoutChild(field, colsWidth[i-j], height); //lay out the field
                x += colsWidth[i-j] + pad;
                h = Math.max(h, field.getHeight());
            }
            w = Math.max(w,x);
            y += h;
        }
        setExtent(w, y);
    }
    
    public int getPreferredWidth()
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();
        int preferredWidth = 0;
        for (int i = 0; i < cols; i++)
            colsWidth[i] = 0;
            
        for (int j = 0; j < numberOfFields; j += cols)
        {
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                int w = field.getPreferredWidth();
                colsWidth[i-j] = Math.max(colsWidth[i-j],w);
            }
        }
        
        int knownWidth = 0;
        int unknownCount = 0;
        
        for (int i = 0; i < cols; i++)
        {
            if (colsWidth[i] == 0x1fffffff)
            {
                unknownCount++;
            }
            else
            {
                knownWidth += colsWidth[i];
            }
        }
        
        if (unknownCount > 0)
        {
            int unknownWidth = (layoutWidth - knownWidth) / unknownCount;
            for (int i = 0; i < cols; i++)
                if (colsWidth[i] == 0x1fffffff)
                    colsWidth[i] = unknownWidth;
        }
        
        for (int i = 0; i < cols; i++)
            preferredWidth += colsWidth[i];
            
        return preferredWidth;
    }
    
    public int getPreferredHeight()
    {
        Field field;
        //get total number of fields within this manager
        int numberOfFields = getFieldCount();
        int preferredHeight = 0;
        for (int j = 0; j < numberOfFields; j += cols)
        {
            int h = 0;
            for (int i = j; i < numberOfFields && i < j + cols;i++)
            {
                field = getField(i); //get the field
                h = Math.max(h,field.getPreferredWidth());
            }
            preferredHeight += h;
        }
        return preferredHeight;
    }
}
Offline  
Old 08-09-2008, 07:14 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks hrbuckley...I'll try this out.

Wow..this is why I despise the java language. So much coding required for such a simple screen layout. I look forward to the day the java standard is replaced.

One question: how do I try out this code? I created a new project but I get a "no entry point" message and no icon appears on my simulator to try it out. Am I suppose to add it as a new java file? If so, how do I call this routine?

Last edited by dynot : 08-09-2008 at 07:38 AM.
Offline  
Old 08-09-2008, 07:55 AM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Wow you are new to Java. Add this file into your project with the name LayoutGrid.java, change the package to match your project. Then create an instance of LayoutGrid(4) to have four columns. Add your objects to the LayoutGrid left to right, top to bottom, then add the LayoutGrid to your screen.
Offline  
Old 08-09-2008, 12:33 PM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2007
Location: montreal
Model: 8100
PIN: N/A
Carrier: rogers
Posts: 70
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

New but learning quickly thanks to members like you.

Works great...thanks again.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.