Table using for dummies


#1

Hi everybody,

I try to use Table by modifying the TableDemo of Juce Demo.

What is don´t understand is how to add a row to the table. In the Demo a XML Model is used. I don´t need that.

So I don´t understand how to fill the model with data. Has anyone a simple table example?

Cheers,
Rüdi


#2

First, you make a class that inherits from TableListBoxModel. This class is responsible for providing the data and also the formatting. The TableDemo example shows some of the advanced features, but in its simples form, you just have to override 3 methods:

#pragma once
#include "jucedemo_headers.h"
class CustomGridModel :
public TableListBoxModel
{
public:
virtual int getNumRows(void);
CustomGridModel(void);
virtual ~CustomGridModel(void);
virtual void paintRowBackground (Graphics& g,
int rowNumber,
int width, int height,
bool rowIsSelected);

virtual void paintCell (Graphics& g,
                        int rowNumber,
                        int columnId,
                        int width, int height,
                        bool rowIsSelected) ;

};

getNumRows is obviously dependent on your data. You can add methods to the model class to let you prime the pump with data before you map it to the TableListBox

paintRowBackground is kind of generic and can look something like:
if (rowIsSelected)
g.fillAll (Colours::yellow);
else g.fillAll (Colours::lightgrey);

and paintCell is the routine where you “draw” your data:
g.setColour (Colours::black);
Font font = 12.0f;
g.setFont (font);

	String text;
	if (columnId == 1)
	{
		text= String(rowNumber + 1);
	}

	g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true);
	g.setColour (Colours::black.withAlpha (0.2f));
	g.fillRect (width - 1, 0, 1, height);

Then, when you create the tableListbox, you give it an instance of this model:

gridModel = new CustomGridModel() ;
table = new TableListBox (T("TableName"),gridModel);
addAndMakeVisible (table);

#3

Many thanks to you!!!


#4

Many thanks to you!!!