I need to extend a JPanel
so that upon a certain condition, it paints a semi-transparent overlay over the whole avaiable space.
From other answers I gathered that in this case, one should override paint()
instead of paintComponent(),
so that I can draw after the components have been painted, but I had no luck overriding either.
Overriding paintComponent()
will draw behind the components on the JPanel
.
Overriding paint() draws in front of them, but moving the mouse over the JButtons
repaints them, and they come on top.
For example this will work, but the JButtons
come on top if hovered:
@Override
public void paint(Graphics g){
super.paint(g); //also tried without this call
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(BK_COLOR);
g2d.fillRect(0,0,getWidth(), getHeight());
}
What am I doing wrong?