# Button Question, strange behavior

**URL:** <https://forum.juce.com/t/button-question-strange-behavior/1786>\
**Category:** General JUCE discussion\
**Created:** [June 12, 2007, 10:55am UTC](https://forum.juce.com/t/button-question-strange-behavior/1786 "2007-06-12T10:55:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayman](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jayman](https://forum.juce.com/u/jayman)\
**Post date:** [June 12, 2007, 10:55am UTC](https://forum.juce.com/t/button-question-strange-behavior/1786/1 "2007-06-12T10:55:58Z")

</div>

I Have a simple button component which is using paintButton, this is what jucer created as below called buttontools.h

[code]#ifndef _BUTTONTOOLS\_H_  
#define _BUTTONTOOLS\_H_

#include \<juce.h\>

//==============================================================================  
/\*\*  
An image based button class  
\*/  
class OnOffButton : public Button

{  
public:

```
//==============================================================================
/** Constructor */
OnOffButton(const String& buttonName);
/** Destructor */
~OnOffButton();

   /** @internal */

```

void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown);

private:

```
//==============================================================================
// (prevent copy constructor and operator= being generated..)
OnOffButton (const OnOffButton&);
const OnOffButton& operator= (const OnOffButton&);

```

};

OnOffButton::OnOffButton (const String& buttonName)  
: Button(buttonName)  
{  
setSize(50,50);  
}

OnOffButton::~OnOffButton()  
{  
}

void OnOffButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)  
{

```
if (getToggleState())
{
	g.setColour (Colour (Colours::red));
    g.fillEllipse (4.0f, 4.0f, 20.0f, 20.0f);
}
else
{
	g.setColour (Colour (Colours::blue));
    g.fillEllipse (4.0f, 4.0f, 20.0f, 20.0f);
}

```

}

#endif[/code]

Im using this like below, its suppose to toggle like and onoff button, works inside juce but does not toggle here!!!, however id does show up, am I missing something? Should be acting like a simple switch

[code]#include “…/…/juce.h”  
#include “buttontools.h”

//==============================================================================  
/\*\* This is the component that sits inside the “hello world” window, filling its  
content area. In this example, we’ll just write “hello world” inside it.  
\*/  
class HelloWorldContentComponent : public Component

{

```
OnOffButton* myonoff;

```

public:  
HelloWorldContentComponent()  
{  
//ONOFF SWITCH  
addAndMakeVisible (myonoff = new OnOffButton (T(“OnOff”)));//Do This First!  
myonoff-\>setBounds(30,60,26,26);  
}

```
~HelloWorldContentComponent()
{
}

void paint (Graphics& g)
{
   
}

```

};[/code]

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 12, 2007, 11:07am UTC](https://forum.juce.com/t/button-question-strange-behavior/1786/2 "2007-06-12T11:07:46Z")

</div>

Yes, you’re missing something - try reading the instructions for the Button toggle methods. You probably want to use Button::setClickingTogglesState.

---

<div class="post-metadata">

**Author:** ![jayman](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jayman](https://forum.juce.com/u/jayman)\
**Post date:** [June 12, 2007, 11:18am UTC](https://forum.juce.com/t/button-question-strange-behavior/1786/3 "2007-06-12T11:18:51Z")

</div>

Thanx, i got confused as jucer subclases from the Button Type not ToggleButton, I just changed the line from Button to ToggleButton now its working.

`
class OnOffButton : public ToggleButton`

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 12, 2007, 11:27am UTC](https://forum.juce.com/t/button-question-strange-behavior/1786/4 "2007-06-12T11:27:23Z")

</div>

…no, it doesn’t have to be a ToggleButton. If you look at the ToggleButton class, you’ll see that it’s just a button that draws itself with a tick-box and automatically calls setClickingTogglesState().

It’s a waste of time making it a ToggleButton unless you want to use its features, so you might as well just call setClickingTogglesState yourself on a normal one.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 3:22pm UTC](https://forum.juce.com/t/button-question-strange-behavior/1786/5 "2017-05-12T15:22:55Z")

</div>

Closed on May 12, 2017, 3:22pm UTC


