Beginner listbox q


#1

Hey All,

I am trying to get a ListBox component going, but I can’t seem to get it to display. My list uses custom components in rows. Here is the code:

class LayerList :	public Component,
			      public ListBoxModel
{
	
private:
	ListBox* list;
	Composition* composition;
	int numRows;
public:
	LayerList(Composition* c);
	int getNumRows();
	void paintListBoxItem(int rowNumber, Graphics &g, int width, int height, bool rowIsSelected);
	Component* refreshComponentForRow (int rowNumber, bool isRowSelected, Component* existingComponentToUpdate);
	
};

LayerList::LayerList(Composition* c) {
	composition = c;
	list = new ListBox(T("Layers List"), this);
	addAndMakeVisible(list);
	//list->updateContent();
	list->setMinimumContentWidth(260);
	list->setRowHeight(50);
	DBG("visible content width " + (String)list->getVisibleContentWidth()); //this returns 0... why?
}

int LayerList::getNumRows() {
	return composition->getNumLayers();
}

void LayerList::paintListBoxItem(int rowNumber, Graphics &g, int width, int height, bool rowIsSelected) {
	DBG("IN PAINT LIST BOX ITEM");   //this method never seems to get called
}

Component* LayerList::refreshComponentForRow (int rowNumber, bool isRowSelected, Component* existingComponentToUpdate) {
	if(getNumRows() > 0) { //don't know if this check is really necessary
		LayerLabelComponent* labelComp = (LayerLabelComponent*) existingComponentToUpdate; //my custom component
		
		if(labelComp == 0) {
			
			DBG("ADDED A LABEL COMP");
			labelComp = new LayerLabelComponent();
		}
		
		DBG("UPDATED A LABEL COMP");
		labelComp->setName(composition->getLayer(rowNumber)->getName());
		DBG( (String)(composition->getLayer(rowNumber)->getName()));
		list->repaintRow(rowNumber);
		return labelComp;
	} else {
		jassert (existingComponentToUpdate == 0);
		return 0;
	}
}

My class LayerList gets added to a DocumentWindow as its content component somewhere further up the food chain. Anything that is glaringly wrong with the way I am doing things?

If I add a component to the LayerList, say a Button in the constructor (or my custom LayerLabelComponent), it will show up in my window, so it isn’t the way I am adding the component to the window.

Any help appreciated, and thanks.

c.


#2

Nothing looks obviously wrong. Did you setVisible (true) on the row components?


#3

In refreshComponentForRow? yes, no difference. If I need to do that somewhere else, let me know.

Any particular reason why paintListBoxItem doesn’t get called? shouldn’t it be called whenever the ListBox is repainted or updated?


#4

Did you call updateContent() after you create it? If the number of rows changes you need to call that to make it refresh.


#5

i did.

the refreshComponentForRow method is getting called, because I see on the console my “UPDATED…” and “ADDED…” messages. It adds, then it updates. But it doesn’t show them.


#6

It’ll be something really simple, but can’t think what at the moment. The debugger is your friend in this kind of situation…


#7

you haven’t shown your resized() function here - have you got one?

the bit you’ve highlighted in the constructor, about visible width returning 0 - that’s because the listbox hasn’t been given a size yet. could be that you’re not setting a size at all.


#8

ahhhh… that was it.

I am still thinking Java, where there is no resized() business. That tripped me up somewhere else before.

many thanks!


#9