ListBox help


Hello all! Could somebody help me?
I’d like to create an elemental list, something like this:

I have written a class TListBox derived from classes ListBox и ListBoxModel, since for me it doesn’t matter to divide data and view:

class TListBox : public ListBox, ListBoxModel
    TListBox(const String& sName = String::empty, ListBoxModel* pModel = 0);
    void paint(Graphics&);
    void resized();

    int getNumRows();
    void paintListBoxItem(int, Graphics&, int, int, bool);
    void listBoxItemClicked(int, const MouseEvent&);
    void listBoxItemDoubleClicked(int, const MouseEvent&);
    void backgroundClicked();
    void deleteKeyPressed(int lastRowSelected);
    TListBoxItem* CreateItem(String);
    Array<TListBoxItem*> pRecords;

    TListBox(const TListBox&);
    const TListBox& operator= (const TListBox&);

In addition, I have created a class TListBoxItem (derived from Component), which keeps and represents the text information of a list’s element (see an image above). In the class TList pointers on elements of the list are stored inthe private member Array<TListBoxItem*> pRecords;

There are several questions:

  1. Where I must create elements of the list (TListBoxItem*)? In the constructor of TListBox?
  2. What the right way to add elemets to the list?
  3. How I must right delete items from the list, when my application stops? deleteAndZero() or something else?


There’s no correct answer… it depends entirely on your app and it’s actually doing!


Well, as I described above, the application isn’t doing nothing especial. 4 items (1 header and 3 elements of the list) with text (see a picture) above. If an item is clicked it became selected / unselected. All.
In a constructor of the class TListBox derived from ListBox and ListBoxModel classes I create a new component (TListBoxItem*) and store the pointer in Array<TListBoxItem*> pRecords; Then I make items visible as we usually do with addAndMakeVisible:

#include "TListBox.h"
#include "TListBoxItem.h"
#define tr(s) String::fromUTF8(s)
TListBox::TListBox(const String& sName, ListBoxModel* pModel) 
    : ListBox(sName, pModel)

    pRecords.add(new TListBoxItem(tr("Header"), (int)Justification::centredLeft));
    pRecords.add(new TListBoxItem(tr("Element 1"), (int)Justification::centredLeft));
    pRecords.add(new TListBoxItem(tr("Element 2"), (int)Justification::centredLeft));
    pRecords.add(new TListBoxItem(tr("Element 3"), (int)Justification::centredLeft));

    setSize(600, 400);

When I try to set the items’ sizes in function resized():

void TListBox::resized()
    pRecords[0]->setBounds(10, 10, 150, 25);
    pRecords[1]->setBounds(10, 45, 150, 25);
    pRecords[2]->setBounds(10, 80, 150, 25);
    pRecords[3]->setBounds(10, 115, 150, 25);

an exception arises at the moment of the programme’s start.
If I transfer the code above in a constructor, an exception arises at the moment of the programme’s end.
What I do incorrectly?


Pretty much everything, I’m afraid.

Why don’t you have a look at the way listboxes are done in the juce demo, and try to copy that?