How to draw rectangle in resized() method?


sorry for silly question. But when I draw rectangle in paint(Graphics &g) it covers all my buttons. But I want that rectangle to be under my buttons (as a background).
My buttons are visible because of use addAndMakeVisible() in my constructor. And I need to set bounds for those buttons and rectangle in resized(). I tried to use addAndMakeVisible(myRectangle) before buttons, but it doesn’t work for Rectangle


What you describe does not make sense. The parent Component paint is called first, then the children Components paint, and the the parent paintOverChildren is called. Maybe you could a simple version of code showing the problem.


Ok, I just used setAlwaysOnTop(true); for my buttons. Now it’s OK. But thanks for support


It may be working, but it still sounds like you are doing something incorrectly. The problem you described should not be happening.


Just for completeness, if somebody else is puzzled:

resized() is a callback to allow you to reposition your child components, or other logic that needs to happen. There is no painting in resized (since there is no graphics context either, I ignore buffer strategies here)

paint() happens in the hierarchy from parent to children, which are painted from back to front.
Siblings are painted in the order they are added by addMakeVisible()
Additionally you can call toFront(), that will move the paint to the end of the queue, over painting all siblings. Same for toBack(), it’s painted first.

There is a chance for a component to paint on top of it’s children, e.g. to implement a Lasso or similar. This is done in the paintOverChildren() callback

Personally I would avoid setting the setAlwaysOnTop flag more than once, since once you have it set several times, you are back to where you started.

And since the question often comes in the same context: you don’t call paint, but you schedule a paint call by calling repaint()


OK, the problem is: I make my Button visible by calling method addAndMakeVisible in the constructor. And setBounds in resized(). And that’s all what I do with my Button.
And I also want some rectangle background for that button (or just frame). So I create Rectangle<int> myRectangle; and setBounds of it in the resized(). Then I call g.drawRect (myRectangle) in the paint().

And that often gives me the effect that my button is covered by myRectangle, and I can’t even see it. Or sometimes I can see it but I can’t click it. I can’t figure it out how I should do to avoid such problems?


Easiest option is to make your Rectangle a component that just paints itself in the paint function. Then, you addAndMakeVisible in the appropiate order (before your button) so it lays under the button. And in the resized() you set its size with setBounds. Now, that’s the fast explanation so I’d suggest looking into this where Jules explains that GUI stuff and how to do in an appropiate way. Worth seeing the whole video tho.

An alternative that comes in my mind if you want your buttons to have that frame you can modify LookAndFeel (there’s a tutorial) to paint the frame on the buttons.