Learning to create new Components

See last post for current status.

FlyingIsFun1217

See last post for current status.

FlyingIsFun1217

Seems simply editing the demo code works just fine.

So right now, I’m looking closer at the code, and seeing where my code is off… sort of merging the changes.

Thanks!
FlyingIsFun1217

Alright, I’ve got my code done (well, layed out and done for the most part, anyway). Here it is:

class songListBox : public Component,
					public TableListBoxModel
{
	private:
		/*--------------------PRIVATE--------------------*/

		TableListBox *sListBox;
		Font *slFont;

		int numOfRows;

		/*--------------------Playlist-Button-Class---------------------*/
		class addToPlaylistButton : public Component,
									public ButtonListener
		{
			private:
				songListBox& owner;
				TextButton *atpButton;

			public:
				addToPlaylistButton(songListBox& owner_) : owner(owner_)
				{
					atpButton = new TextButton(T("Add to Playlist"));
					addAndMakeVisible(atpButton);

					atpButton -> addButtonListener(this);
				}

				~addToPlaylistButton()
				{
					deleteAllChildren();
				}

				void resized()
				{
					atpButton -> setBoundsInset(BorderSize(2));
				}

				void buttonClicked(Button *buttonThatWazClicked)
				{
					if(buttonThatWazClicked == atpButton)
					{
						//
					}
				}
		};
		/*--------------------Playlist-Button-Class---------------------*/

		/*--------------------PRIVATE--------------------*/
    public:
		/*--------------------PUBLIC--------------------*/
		songListBox()
		{
			// Add it when initialized
			addAndMakeVisible(sListBox = new TableListBox(T("Song List Table"), this));

			// Add a border with specified colors
			sListBox -> setColour(ListBox::outlineColourId, Colours::black);
			sListBox -> setOutlineThickness(2);

			// Create columns
			// Need to look through docs more to understand how this works, what it does, etc.
			// sListBox -> getHeader() -> addColumn();
		}

		~songListBox()
		{
			deleteAllChildren();
		}

		int getNumberOfRows()
		{
			return numOfRows;
		}

		void paintRowBackground (Graphics& g, int rowNumber, int width, int height, bool rowIsSelected)
		{
			if (rowIsSelected)
			{
				g.fillAll (Colours::azure);
			}
		}

		void paintCell(Graphics& g,
					   int rowNumber,
					   int columnID,
					   int width, int height,
					   bool rowIsSelected)
		{
			g.setColour(Colours::black);
			// g.setFont(slFont);

			// Here's where we would draw any actual text
		}

		Component *refreshComponentForCell(int row, int columnID, bool isRowSelected,
										   Component *existingComponentToUpdate)
		{
			if(columnID == 4)
			{
				addToPlaylistButton *plButton = (addToPlaylistButton*)existingComponentToUpdate;

				if(plButton == 0)
				{
					plButton = new addToPlaylistButton(*this);
				}

				return plButton;
			}

			else
			{
				jassert (existingComponentToUpdate == 0);
				return 0;
			}
		}

		int getColumnAutoSizeWidth(int columnID)
		{
			if(columnID == 4)
			{
				return 150;
			}
		}

		void resized()
		{
			sListBox -> setBoundsInset(BorderSize(8));
		}
		/*--------------------PUBLIC--------------------*/
};

/*------------------------------------------------------------*/

Component *createSongListBox()
{
	return new songListBox();
}

Now when I try to use it in my main component like such:

class MainComponent  : public Component,
                       public songListBox
{
    private:

        songListBox *mainSongList;

    public:

        MainComponent()
        {
            //
            mainSongList = new songListBox();
            addAndMakeVisible(mainSongList);
            mainSongList -> setBounds(10, 10, 100, 100);
        }

        ~MainComponent()
        {
            deleteAllChildren();
        }

        void resized()
        {
            //
        }

        void paint(Graphics& g)
        {
            //
        }
};

I get some errors:

Please, if you could, help me. Guide me. Let me learn how to do things with this amazing library!

Thanks!
FlyingIsFun1217

The first problem I see is that TableListBoxModel which your songListBox iherits from has a three pure virtual functions:

and

and

This makes TableListBoxModel absract. You must define one of these in your class.

Alright, I’ll check that over.

Thanks!
FlyingIsFun1217

Had quite a bit of homework to do; couldn’t get to this much sooner than now.

Seems the only function that I forgot to overload from TableListBoxModel was getNumRows (which I replaced getNumberOfRows with). This brings me down to somewhat manageable (in appearance) errors:

as to songListBox not being a type, it is referring to:

Component *createSongListBox()
{
	return new songListBox();
}

Thanks for helping me out, this is a great learning experience (both in this API and furthering my admittedly basic C++ skills)!

FlyingIsFun1217

So what does the code look like now?

Exactly the same, with getNumberOfRows just renamed to getNumRows.

FlyingIsFun1217

ok.

... class MainComponent : public Component, public songListBox { ...
you don’t need MainComponent to derive from songListBox, your creating and songListBox to MainComponent.

so change it to.

... class MainComponent : public Component { ...

The above will fix the ambiguous access of ‘addAndMakeVisible’ error.
Your MainComponent derives from Component, and then you want to derive from songListBox ?!@. songListBox derives from component too so addAndMakeVisible function is ambiguous.

Take a look at the documentation also, I have it open as I work all the time.

Ok, I see… I shouldn’t be deriving from components that are just being added, right?

[quote=“aiit”]
Take a look at the documentation also, I have it open as I work all the time.[/quote]

There is Actual Documentation?! Or do you just mean the API docs?

FlyingIsFun1217

You shouldn’t be deriving from Component twice.

[quote]There is Actual Documentation?! Or do you just mean the API docs?
FlyingIsFun1217[/quote]

http://www.rawmaterialsoftware.com/juce/api/index.html

You shouldn’t be deriving from Component twice.
[/quote]

Ahh, I get it. :smiley:

[quote=“aiit”]

[quote]There is Actual Documentation?! Or do you just mean the API docs?
FlyingIsFun1217[/quote]

http://www.rawmaterialsoftware.com/juce/api/index.html[/quote]

Yeah, you meant the API docs.

Thanks!
FlyingIsFun1217

Alright, removing the songListBox inheritance actually produced a couple more errors:

Again, the source is the same except for the renamed getNumberOfRows and the removed songListBox inheritance.

Thanks!
FlyingIsFun1217

Can it be an include problem?
How many files do you have?
Do you have songListBox.h and a songListBox.cpp?
Is MainComponent in a separate file?
Are you including songListBox.h in the MainComponent file?

I tried it here and it compiles just fine. All I did was copy your code into the example juce_application’s main.cpp file.

Well, I’ll back up the source and try to compile it clean.

Thanks for confirming! Now I just need to work on event handlers and a way to add strings to specific areas in the table ‘grid’, and I’ll have the basis for my first Juce application.

Thanks!
FlyingIsFun1217

Alright, I just can’t figure out what could be causing the errors at this point.

I’ve uploaded my project (C::B) with all source. All libraries do link, it’s only the custom component that’s producing errors. The zip can be found here (under other, called ‘elements.zip’).

Thanks again for the time and effort, I appreciate it greatly!
FlyingIsFun1217

fixed code http://www.drop.io/forumCode/media

Can I ask what was changed? Was it another little stupid mistake on my part?

I’ll download it now; thanks for taking the time to help again!

FlyingIsFun1217