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
Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Usage of invalidate method

Hello

 

Can someone tell me what is the exact usage of invalidat() method in implementation.

 

BB docs mentions that 

protected void invalidate() 
          Marks this entire field invalid.
protected  void	invalidate(int x, int y, int width, int height) 
          Marks a region of this field as invalid (requiring repainting).
protected  void	invalidateAll(int x, int y, int width, int height) 
          Marks a region of this field as invalid (requiring repainting).

 I have a grid field with some rows and columns.How can i use invalidate in my case so that it refreshes the grid,that is,repaints the grid values.

 

Is it really possible to get done with grid field.

 

Please help guys.

 

Thanks

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Usage of invalidate method

I have not used a Grid Field, I am not even sure what you mean.  So I'm not sure what I have to say is applicable.

 

But in my experience, an invalidate() call is used as an indication to the Field that it needs to repaint itself, i.e. that the paint() method needs to be called. So it is not used when you want to change the contents of a Field, it is used when something effects the way a Field will look.  

 

One example is focus change.  If you do special processing in your Field to handle focus, then you may call invalidate() in the onFocus and onUnfocus methods so that paint() gets called and the state of the Field is accurately represented. 

Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Usage of invalidate method

Hi peter,

Thanks for responding.

I actually am retrieving records from my database table and displaying into my form using GridFieldManager.

GridFieldManager is used to represent data in grid/table format.

I wanted some way to update my tab screen which holds this GridFieldManager so that the grid contains new values after every invalidate() call

But from what i learned through ur exaplanation, i dont think i can used this method with gridfield to refresh display.

Are you aware of any other method to go about doing updation of screen/tabs.

Thanks.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Usage of invalidate method

Let us take a simple example.  If you want to update a LabelField, then you can do that with

<labelField>.setText(<new Text>)

This will update the contents of the labelField and automatically cause an update to the screen.

 

While this does make it easy, this can actually cause a perceived slow down if you do this a lot.  Say you added 100 LabelFields to the screen, and then you updated all of them.  You will do the update individually, and for each of the updates, the screen would get refreshed.  So that is 100 screen updates.

 

Instead of doing this,when you are updating loads of Field, I recommend that you create new Fields and add them to a new Manager.  Then when you have finished, you can just replace the old Manager with a new one .  So just one screen update. And in fact you can update the Manager without being on the Event Thread, if the Manager is not displayed.  So the only bit of the processing that needs to be on the Event Thread is the replace.  So doing the screen update this way is significantly faster.

 

Give it a try. 

Regular Contributor
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Usage of invalidate method

Thanks peter.That was a nice explanation.

 

I would like to know how can i go about replacing manager.

 

Like say i have a grid field manager that accepts grid values.

 

Same procedure i use to fill another grid.

 

Then how can i replace the previous grid with the new one.I actually want this action to be triggered on some button click.

 

This is my code for grid and inserting values into grid from db table:

final GridFieldManager grid = new GridFieldManager(100,2,0); 
 
        grid.add(new LabelField("Customer Name")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
         
         System.out.println("Grid for name added");
        grid.add(new LabelField("Total Outstanding")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
     

        grid.setColumnPadding(100);
        grid.setRowPadding(20);
        
                    
                        try
                      {
                      //Open or create the database
                        Database db = DatabaseFactory.openOrCreate("database1.db"); 
         
                        Statement statementG55 = db.createStatement("CREATE TABLE IF NOT EXISTS Temp4(gname TEXT,gbal INTEGER)");
                        statementG55.prepare();
                        statementG55.execute();       
                        statementG55.close();
         
                        Statement statementG56 = db.createStatement("SELECT gname,gbal FROM Temp4 ORDER BY ROWID DESC");
                        statementG56.prepare();
                        statementG56.execute();

                                Cursor c = statementG56.getCursor();
                             
                                //Get to the row of grid
                                 for (int i =1; i < grid.getRowCount(); i++)
                                 {
                                     
                                        System.out.println("Inside for first loops");
                                        //Get to the column of grid
                                    for (int j = 0; j < grid.getColumnCount() ; j++)
                                    {
                                       System.out.println("Inside for second loops");
                                       //Get to the row of temp4 table
                                       while(c.next()) 
                                       {
                                          
                                          System.out.println("Inside while"); 
                                            Row r;
                                            r = c.getRow();
                                            //Get to the column of temp4 table
                                            //grid.insert(new LabelField("" +i),i);
                                            for (int k = 1; k >=0; k--)
                                            {
                                                
                                                System.out.println("Inside for loops");
                                                //Check for whether column retrieved is date or naraation
                                                if(k==0)
                                                {
                                                    System.out.println("Retrieving Names");
                                                    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);
  
                                                    grid.insert(new LabelField(p)
                                                    {
                                                        public void paint(Graphics graphics)
                                                        {
                                                        graphics.setColor(Color.GOLD);
                                                        super.paint(graphics);
                                                        }
                                                     },i,j); 
  
                                                     grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.DODGERBLUE,Color.MIDNIGHTBLUE,Color.DODGERBLUE));                 
                                                  } 
                                                  System.out.println("Exiting while");                        
                                               }
                                          
                                              System.out.println("Exiting sec for");
                                              break;
                                          }
                                    System.out.println("Exiting first for");
                                    break;
                                   }
                                   } 
                                   statementG56.close(); 
                                   db.close();
                      } 
                      catch(Exception e) 
                      {         
                            System.out.println( e.getMessage() );
                            e.printStackTrace();     
                      }             
    }
add(grid);      

 

 

Would appreciate if you cud demonstrate a coding construct.

 

Thanks.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Usage of invalidate method

You replace it in the Manager or screen that it has been added to.

 

For example, say I have Field f1 and I want to replace it with Field F2.  Here is some code to do that:

 

Manager m = f1.getManager();

m.replace(f1, f2);

 

Older OS's did not include the replace method, in which case you find the index of the Field to be replaced, delete the old Field and insert the new Field at that index.

 

Since Manager extends Field, all this applies to Managers as well. 

Regular Contributor
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Usage of invalidate method

Can i overwrite over the same field.
This just happened to cross my mind that since i am at present updating the results in the same manager,what if i replace the field1 with field1 again.Will that work?
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Usage of invalidate method

"Can i overwrite over the same field"

 

Yes, but you will hit the performance problem I metioned previously.

Regular Contributor
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Usage of invalidate method

Do u have any idea how i can invalidate a field of another tab from the present tab.

Like say i have this grid field in second tab.Now i want to invalidate it some function that invokes this invalidate from first tab.

Any idea?

Thanks.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Usage of invalidate method

Sorry you need to use another term.  As we have already discussed, invalidate has a specific meaning in BlackBerry and the invalidate() method is not going to achieve what you want.

 

Let us call the process you are trying to do 'rebuild'.  In other words, you want to rebuild the another tab. 

 

There is no built in BlackBerry method that will do that, but then there was no built in method that created the other tab in the first place was there?  You had to do it.  So the rebuild process just repeats this process.  So you will need to have a way of initiating this rebuild method from your other tab.  This is a standard Java issue (passing references round) and there is nothing BlackBerry specific for doing this, nor any need for there to be. 

 

So focus on doing this in Java, rather than  looking for a specific BlackBerry solution. 

 

Good luck.