refreshComponentForRow problems


#1

Hello!

I have a folder with 11 wav files and 2 folders:

Nueva Carpeta [folder]
SYM54 [folder]
SYM_STR_C_2.wav
SYM_STR_C_3.wav
SYM_STR_C_4.wav
SYM_STR_C_5.wav
SYM_STR_C_6.wav
SYM_STR_C_7.wav
SYM_STR_F_2.wav
SYM_STR_F_3.wav
SYM_STR_F_4.wav
SYM_STR_F_5.wav
SYM_STR_F_6.wav

  1. Why rowNumber from paintListBoxItem goes from zero to 13 while rowNumber in refreshComponentForRow goes from zero to 14? (crashing my program because 14 doesn’t exist in the owned array called m_Archivos)
Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate)
{
	return new CBrowserItem(m_Archivos->NombreArchivo(rowNumber));
}
  1. If I understand right, existingComponentToUpdate shouldn’t be null when the componet is already created. Then, when I run my program, why I see weirds results when I use this method?
Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate)
{
	if (existingComponentToUpdate == 0)
	{
		return new CBrowserItem(m_Archivos->NombreArchivo(rowNumber));
	}
	else 
	{
		return existingComponentToUpdate;
	}
}

Results:
Nueva Carpeta
SYM54
SYM_STR_C_2
SYM_STR_C_3
SYM_STR_C_4
SYM_STR_C_5
SYM_STR_C_6
SYM_STR_C_7
SYM_STR_F_2
SYM_STR_F_3
SYM_STR_F_4
Nueva Carpeta //<----what’re doing those here!?!
SYM54


#2

This condition would be useful

int totalItems =  ownedArray.size ();
if (rowNumber >= 0 && rowNumber < totalItems )
 // do your thing

It’s a good practice to use these end conditions.

are you setting the number of rows? You can do that using
int TableListBox::getNumRows ( )

Normally components are deleted, when they are no longer shown. If you can keep a break point/logger message in the destructor of “CBrowserItem”,
you can determine whether the items deleted and then recreated.


#3

Uhmm thx for your answer! however that didn’t solve the problem:

Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate)
{
	int TotalItems = m_Archivos->Tamaño(); //Tamaño return the size of the owned array
	if (rowNumber >=0 && rowNumber<TotalItems)
	{
		if (existingComponentToUpdate == 0)
		{
			return new CBrowserItem(m_Archivos->NombreArchivo(rowNumber));
		}
		else 
		{
			return existingComponentToUpdate;
		}
	}
}[/code]

Now I get an exception in this Component method:
[code]void Component::addAndMakeVisible (Component* const child, int zOrder)
{
    if (child != 0)
    {
        child->setVisible (true); //here I get the exception!
        addChildComponent (child, zOrder);
    }
}

CBrowserItem is a component so… :?:


#4

This might help.

Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate)
{
   int TotalItems = m_Archivos->Tamaño(); //Tamaño return the size of the owned array
   if (rowNumber >= 0 && rowNumber < TotalItems)
   {
      if (existingComponentToUpdate == 0)
      {
         existingComponentToUpdate =  new CBrowserItem(m_Archivos->NombreArchivo(rowNumber));
      }
   }
   else
        existingComponentToUpdate = 0L;

return existingComponentToUpdate;
}

It’s simpler to debug, if your method has single return statement.
put a logger message in “getNumRows ( )” method and check what do you return 13 or 14. It should be 13.


#5

Thx again man, however I continue with problems. I’ve used a simple DBG macro here:

Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate) { String strlog(rowNumber); int TotalItems = m_Archivos->Tamaño(); if (rowNumber >=0 && rowNumber<TotalItems) { if (existingComponentToUpdate == 0) { existingComponentToUpdate = new CBrowserItem(m_Archivos->NombreArchivo(rowNumber)); DBG("Exist: " + strlog); } else { existingComponentToUpdate = 0L; DBG("DOESNT Exist: " + strlog); } } return existingComponentToUpdate; }

Then I run the program and when I resize the window (or move the scrollbar) sometimes existingComponentToUpdate is null, sometimes isn’t.

Take a look, I run the program and I get this window:

Now I move the scrollbar down:

Now I stop the program and run it again, but this time I resize the bottom of the window:

Now I resize the width of the window only one step

Thanks you, any help will be highly appreciated!!


#6

You have done a mistake

Component* CBrowserModelo::refreshComponentForRow(int rowNumber, bool isRowSelected, Component *existingComponentToUpdate)
{
   String strlog(rowNumber);
   int TotalItems = m_Archivos->Tamaño();
   if (rowNumber >=0 && rowNumber<TotalItems)
   {
      if (existingComponentToUpdate == 0)
      {
         existingComponentToUpdate = new CBrowserItem(m_Archivos->NombreArchivo(rowNumber));
         DBG("Exist: " + strlog);
      }
// don't do this, you are initializing a valid pointer to null
    /*  else
      {
         existingComponentToUpdate = 0L;
         DBG("DOESNT Exist: " + strlog);
      }*/
   }
  // add code here
   else
     existingComponentToUpdate = 0L;
   return existingComponentToUpdate;
} 

#7

omg now I understand, I must do null all the row that aren’t in range. This works. Thank you vishvest, you’re really kind! thx!! :smiley:


#8

let me explain how table list box works.

Deletion :
Everytime you resize the window containing the listbox or scroll up/down components shown in the list box are deleted.

Let us assume that 10 rows are visible

Now if the window is resized(made smaller), and only 3 rows visible, juce would delete 5 rows, sparing 3 rows which are visible plus two other which follow the visible rows.

Now again if you resize the window(made bigger), making 10 rows visible juce has to create these components again. This is the reason components are created repeatedly on window resize and listbox scroll.

Great. I got a lot of help when I had problem. Just returning a favour I got from others. :smiley:


#9

Thx man, I understand! :wink: