Just a simple question about combobox, rectangle and graphics

gui

#1

Hello everyone! I am just trying to understand how to work with combobox and i decided to create a simple application where you can change colour of a rectangle by selecting one of them from combobox. And i stuck here. I don’t know how to change rectangle colour because i suppose than i can do it only in paint function and i don’t know how to transfer the colour from combobox to it. If you know other ways to do it please tell me. Thank you!

So i created combobox and rectangle:

ComboBox combo1;
Rectangle rect; // it is int

Colours for combobox:

enum ColoursForCombo
{
red = 1,
lime,
orange,
yellow
};

Constructor:

MainComponent::MainComponent()
{
setSize(600, 400);

addAndMakeVisible(combo1);
addAndMakeVisible(button);

combo1.setBounds(100, 100, 100, 20);
rect.setBounds(200, 200, 100, 100);

combo1.addItem(“red”, red);
combo1.addItem(“lime”, lime);
combo1.addItem(“orange”, orange);
combo1.addItem(“yellow”, yellow);
combo1.onChange = [this] {selectedID(); };
combo1.setSelectedId(yellow);
}

And selectedID function:

void selectedID()
{
Colour c;
switch (combo1.getSelectedId())
{
case lime: c = Colours::lime; break;
case red: c = Colours::red; break;
case orange: c = Colours::orange; break;
case yellow: c = Colours::yellow; break;
}
}


#2

I think the simplest way to is as follows:

  • move the contents of your ‘selectedID()’ to your paint method. So everytime your GUI is repainted, it chooses the right color according to the comboBox
  • call repaint() when the comboBox changes

JUCE lets the operating system decide, when to call paint(), you “only” can flag your component as “dirty” by calling repaint(). Once the OS has time to do something it checks if an area or a component is “dirty”, if yes it’s calling the paint method


#3

@danielrudrich, Thank you!