I create an AUDIO application using the project template, and JUCE creates an AudioAppComponent. When I run this simple application, the component background is black and whatever I add in it (such as a Button with an addAndMakeVisible method), nothing appears (et least I suppose it’s hidden by the black background). I am pretty sure it’s something stupid. I am using the latest version of Juce and test it on Mac and Linux.
Try the following:
g.fillAll (Colours::red); // in void paint (Graphics& g) override
change the colour to your desired colour.
This should change the background colour.
Thanks, I knew it was something stupid!
I have another (also stupid I guess) question: the TextButton I added didn’t show up. Must I call a specific method in “void paint (Graphics& g) override” in order to draw it once the background color is set?
Do you have something like the following in the constructor:
addAndMakeVisible (textButton = new TextButton (“new button”));
textButton->setButtonText (TRANS(“Changed Text”));
There should be more code in the destructor as well as implementing the listener.
Yes I create a TextButton that way (I just don’t have implement a listener but it shouldn’t influence the rendering).
You will need something like this in the destructor
textButton = nullptr;
a code like this in the resize
textButton->setBounds (64, 50, 150, 24); // numbers can be replaced by getBounds() if I am not wrong.
N.B. You don’t need that. If your textButton is a
ScopedPointer<Textbutton> textButton; then it will be deleted when the textButton goes out of scope, i.e. when the destructor is done.
If your textButton is a raw pointer
TextButton* textButton; then setting it to nullptr does not destroy the object on the heap, i.e. you are leaking memory. In this case you need to call
delete textButton; (but it’s way better to use ScopedPointer instead!)
You only need to set
textButton = nullptr; if you need to make sure, that the objects are destroyed in a certain sequence, which is very rarely the case.
yes, that’s the essential part (I forget it very often myself)
Thanks, it works now. As expected, my question was again stupid. In fact, I supposed that Component::addAndMakeVisible implies some kind of layout manager that automatically resizes and places the child components.
Please note that this is how Add New GUI Component (in Projucer) implements a button if it is added using the Graphic Subcomponents Tab.
Should this be reported as a bug or something that needs changing?
I don’t know, if it is considered a bug. It’s simply not necessary, but it also doesn’t hurt.
The whole benefit of RAII and ScopedPointer is, that you don’t need to care about destruction, that’s why I would leave it out until it’s really needed.
But there are certainly a lot of opinions on that topic.