1

How to add margins to the grid elements in grid manager layout of blackberry.I want it to appear exactly like it looks on a sqlite table with rows and columns having neat margins.Also i would like to add color to each row of the grid.The elements in the grid have been fetched from sqlite table string by string.

I have the following code:

GridFieldManager grid = new GridFieldManager(10,5,0); 

grid.add(new LabelField("Date"));
grid.add(new LabelField("Bill"));
grid.add(new LabelField("Receipt"));
grid.add(new LabelField("Narration"));
grid.add(new LabelField("ID"));

grid.setColumnPadding(15);
grid.setRowPadding(20);

for (int i = 1; i < grid.getRowCount(); i++) {
  System.out.println("Inside for first loops");
  for (int j = 0; j < grid.getColumnCount() ; j++) {
    System.out.println("Inside for second loops");

    while(c.next()) {                                          
      System.out.println("Inside while"); 
      Row r;
      r = c.getRow();

      for (int k = 4; k >=0; k--) {           
        System.out.println("Inside for loops");

        if(k==0 || k==3) {
          System.out.println("Retrieving date or narration");
          grid.insert(new LabelField(r.getString(k)) {
            public void paint(Graphics graphics) {
              graphics.setColor(Color.BROWN);
              super.paint(graphics);
            }
          },i,j);
        } else {
          System.out.println("Retrieving other values"); 
          String p = "" + r.getInteger(k);
          grid.insert(new LabelField(p) {
            public void paint(Graphics graphics) {
              graphics.setColor(Color.BROWN);
              super.paint(graphics);
            }
          },i,j);
        }   
        grid.setBackground(
          BackgroundFactory.createLinearGradientBackground(
            Color.GOLD,Color.CHOCOLATE,Color.GOLDENROD,Color.CORAL));
      } 
      System.out.println("Exiting while");                        
    }

    System.out.println("Exiting sec for");
    break;
  }
  System.out.println("Exiting first for");
  break;
} 

With the above code I am getting a linear gradient background color being applied to the whole screen. Also the elements of the grid excluding the headings are in brown color.

What I now want to achieve is separate colors for each row. Anyone aware on the solution to this please share. Thanks.

learning_fly
  • 382
  • 1
  • 2
  • 11

2 Answers2

0

You can achieve the color part in two ways:

  1. Override the grid manager paint method (difficult)
  2. Add a custom VerticalFieldManager containing exactly one label field to each cell, instead of adding the labelfield directly. This is the easiest approach IMHO.
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
0

To add color to the fetched rows you could use this code:

  for (int k = 4; k >=0; k--)
                                        {

                                            System.out.println("Inside for loops");
                                            //Check for whether column retrieved is date or naraation
                                            if(k==0 || k==3)
                                            {
                                                System.out.println("Retrieving date or narration");
                                                grid.insert(new LabelField(r.getString(k))
                                                {
                                                    public void paint(Graphics graphics)
                                                    {
                                                    graphics.setColor(Color.GOLD);
                                                    super.paint(graphics);
                                                    }
                                                 },i,j);


                                            }  
                                            else
                                            {   
                                                //Check for whether column retrieved is bills,rec or id
                                                System.out.println("Retrieving other values"); 
                                                String p = "" + r.getObject(k);

                                                //if(r.getString(k) != null)
                                                //{ 
                                                grid.insert(new LabelField(p)
                                                {
                                                    public void paint(Graphics graphics)
                                                    {
                                                    graphics.setColor(Color.GOLD);
                                                    super.paint(graphics);
                                                    }
                                                 },i,j); 
                                               //  } 


                                            }   

Using it in if else manner may help your solution.

bbDiva
  • 51
  • 5