Beginner: component not visible


#1

I’ve been trying to get to grips with the basics of Juce. With the help of the introjucer I created a basic window, then working in Visual Studio 2010 added my own component to it with a button in. It compiles fine but when run I just get an empty window.

main.cpp

[code]#include “…/JuceLibraryCode/JuceHeader.h”
#include “MainWindow.h”

class JuceTestApplication : public JUCEApplication
{
public:
JuceTestApplication()
{
}

~JuceTestApplication()
{
}

void initialise (const String& commandLine)
{
    mainWindow = new MainAppWindow();
}

void shutdown()
{
    mainWindow = 0;
}

void systemRequestedQuit()
{
    quit();
}

const String getApplicationName()
{
    return "JuceTest";
}

const String getApplicationVersion()
{
    return ProjectInfo::versionString;
}

bool moreThanOneInstanceAllowed()
{
    return true;
}

void anotherInstanceStarted (const String& commandLine)
{
    
}

private:
ScopedPointer mainWindow;
};

START_JUCE_APPLICATION(JuceTestApplication)
[/code]

MainWindow.h

[code]#ifndef MAINWINDOW_H_3280BB8E
#define MAINWINDOW_H_3280BB8E

#include “…/JuceLibraryCode/JuceHeader.h”
#include “MainComponent.h”

class MainAppWindow : public DocumentWindow
{
public:
MainAppWindow();
~MainAppWindow();

void closeButtonPressed();

private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainAppWindow)
};

MainAppWindow::MainAppWindow()
: DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
Colours::lightgrey,
DocumentWindow::allButtons)
{
setContentOwned (new MainComponent(), true);
centreWithSize (700, 500);
setVisible (true);
}

MainAppWindow::~MainAppWindow()
{
}

void MainAppWindow::closeButtonPressed()
{
JUCEApplication::getInstance()->systemRequestedQuit();
}

#endif // MAINWINDOW_H_3280BB8E
[/code]

MainComponent.h

[code]#ifndef MAINWINDOW_H_3280BB8E
#define MAINWINDOW_H_3280BB8E

#include “…/JuceLibraryCode/JuceHeader.h”
#include “MainComponent.h”

class MainAppWindow : public DocumentWindow
{
public:
MainAppWindow();
~MainAppWindow();

void closeButtonPressed();

private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainAppWindow)
};

MainAppWindow::MainAppWindow()
: DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
Colours::lightgrey,
DocumentWindow::allButtons)
{
setContentOwned (new MainComponent(), true);
centreWithSize (700, 500);
setVisible (true);
}

MainAppWindow::~MainAppWindow()
{
}

void MainAppWindow::closeButtonPressed()
{
JUCEApplication::getInstance()->systemRequestedQuit();
}

#endif // MAINWINDOW_H_3280BB8E
[/code]

MainComponent.cpp

[code]#include “MainComponent.h”

MainComponent::MainComponent ()
{
centreWithSize (200, 200);
setVisible(true);
addAndMakeVisible(Button1 = new TextButton());
Button1->setButtonText(“It’s a button”);
Button1->addListener (this);

}

MainComponent::~MainComponent()
{
deleteAndZero(Button1);
}

void MainComponent::buttonClicked (Button* buttonThatWasClicked)
{
if (buttonThatWasClicked == Button1)
{
Button1->setButtonText(“You clicked it”);
}
}[/code]

I know I’ve just missed something stupid, so hopefully someone can spot it quickly :slight_smile:

thanks in advance


#2

Where exactly were you expecting this button to appear? Try calling setBounds() on it!


#3

haha, I knew it was something stupid.

Thanks a lot, it works fine now :smiley: