TextEditorListener


#1

I’m confused as to how to add a TextEditorListener to my Class MainComponent : public Component. When I try to add the TextEditorListener, GCC complains that the following virtual functions are abstract and the compile fails:

textEditorTextChanged (TextEditor &editor)
textEditorReturnKeyPressed (TextEditor &editor)
textEditorEscapeKeyPressed (TextEditor &editor)
textEditorFocusLost (TextEditor &editor)

What do I do? I’m a relatively experienced C programmer, but a very inexperienced C++ programmer.

Thanks… Gary.


#2

you need to impelement those functions.

like…

MainComponent ::textEditorTextChanged (TextEditor &editor)
{
do stuff with editor
}
MainComponent ::textEditorReturnKeyPressed (TextEditor &editor)
{
do stuff with editor
}
MainComponent ::textEditorEscapeKeyPressed (TextEditor &editor)
{
do stuff with editor
}
MainComponent ::textEditorFocusLost (TextEditor &editor)
{
do funky stuff with anything
}


#3

they are whats known as “pure virtual” functions.

defined as so…

virtual int functionname(void)=0;

in the base class. these MUST be provided by subclasses.


#4

I know they need to be implemented if I intend to use them… but I tried this:

class MainComponent : public Component, public TextEditorListener { private: TextEditor* texted; public: MainComponent() : Component( T("Main Component") ) { texted = new TextEditor (T("Text Editor"), 0); addAndMakeVisible (texted); texted->setBounds (20, 20, 260, 235); texted->addListener (this); } ~MainComponent() { deleteAllChildren (); } void textEditorTextChanged (TextEditor* texted) { AlertWindow::showMessageBox (AlertWindow::InfoIcon, T("TextEditor"), T("Just got a message!")); } etc...

and got the following errors:

[list]C:\juce\docs\Tutorial>mkgcc te
te.cpp: In function juce::Component* createComp()': te.cpp:77: error: cannot allocate an object of typeMainComponent’
te.cpp:77: error: because the following virtual functions are abstract:
c:/juce/src/juce_appframework/audio/devices/…/…/gui/components/controls/juce_T
extEditor.h:57: error: virtual void juce::TextEditorListener::textEditorTextCha
nged(juce::TextEditor&)
c:/juce/src/juce_appframework/audio/devices/…/…/gui/components/controls/juce_T
extEditor.h:60: error: virtual void juce::TextEditorListener::textEditorReturnK
eyPressed(juce::TextEditor&)
c:/juce/src/juce_appframework/audio/devices/…/…/gui/components/controls/juce_T
extEditor.h:63: error: virtual void juce::TextEditorListener::textEditorEscapeK
eyPressed(juce::TextEditor&)
c:/juce/src/juce_appframework/audio/devices/…/…/gui/components/controls/juce_T
extEditor.h:66: error: virtual void juce::TextEditorListener::textEditorFocusLo
st(juce::TextEditor&)[/list]

Gary.


#5

no. you MUST implement those functions!

you must make concrete an abstract class.


#6

[code]class MainComponent : public Component,
public TextEditorListener
{
private:
TextEditor* texted;
public:
MainComponent()
: Component( T(“Main Component”) )
{
texted = new TextEditor (T(“Text Editor”), 0);
addAndMakeVisible (texted);
texted->setBounds (20, 20, 260, 235);
texted->addListener (this);
}
~MainComponent()
{
deleteAllChildren ();
}

void textEditorTextChanged (TextEditor &editor)
{
AlertWindow::showMessageBox (AlertWindow::InfoIcon,
T(“TextEditor”),
T(“Just got a message!”));
}
void textEditorReturnKeyPressed (TextEditor &editor)
{
///do stuff with editor
}
void textEditorEscapeKeyPressed (TextEditor &editor)
{
///do stuff with editor
}
void textEditorFocusLost (TextEditor &editor)
{
///do funky stuff with anything
}
[/code]

that’ll work. i tink


#7

Do you have a complete example that you could share. Like I said I’m really inexperienced at C++ and I don’t understand how to do what you’re telling me.


#8

oops… you sent an example while I was typing.


#9

OK… that worked, but I’m confused. Why don’t I have to do the same thing with the ButtonListener Class? It has 2 virtual functions and i don’t have to define both of them in my program… only the one I want to use… ie. void buttonClicked (Button* button). Why don’t I have to define void buttonStateChanged (Button *button) also?


#10

those are virtual, but they are not pure virtual. its the =0 bit at the end of a definition that makes it pure.

a pure vitral function has no implementation. it only defines an interface. the definition of an abstract class is a class that has at least one pure vitual function.

for example Thing might be an abstract class, and Toilet may be a concrete class (IE not abstract) derived (extends or inherets from) from Thing.

Toilet is a Thing. But not all Things are Toilets!

For example you could have a list of Things. some of them could be Toilets. but the list sees them all as Things.


#11

read this…

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html


#12

Thanks… that makes it clear. I need to look more deeply into my C++ docs. It’s mainly a matter of semantics. I’ll learn it eventually.

Thanks again… Gary.

BTW… I have TICPP in my docs folder. I just need to read it more :wink:


#13