jn025
jn025

Reputation: 2895

Java repaint hiding other panel

I have 3 Jpanels nested like the following:

main JPanel
{
  second JPanel {}
  3rd JPanel {}
  main method
}

This is the order I have added them in the 'main panel' :

    add(thirdPanel);
    add(secondPanel);
    setComponentZOrder(componentsPanel, 0);
    setComponentZOrder(panel, 1);

The second and third panels both have paintComponent methods and initially the thirdPanel which contains GUI components such as buttons etc. is on top of the secondPanel (which is what I want) but whenever I draw or repaint in the secondPanel, all the components in thirdPanel are hidden beneath the secondPanel. It's like secondPanel is taking priority. I basically want the thirdPanel to stay on top no matter what.

Note: I am using a null layout manager.

Upvotes: 0

Views: 357

Answers (1)

Boann
Boann

Reputation: 50041

Override the isOptimizedDrawingEnabled method of the parent panel and have it return false:

@Override
public boolean isOptimizedDrawingEnabled() {
    return false;
}

This is necessary if a component's children overlap, otherwise Swing will not paint them correctly.

Upvotes: 2

Related Questions