Java repaint hiding other panel
Asked Answered
M

1

0

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.

Mitrewort answered 22/8, 2014 at 8:5 Comment(4)
setComponentZOrder is done by LayoutManager, not required, btw whats is your question (based on description and code posted)Amaral
Sorry if Q was a little unclear. Q: How do I stop the secondPanel from overriding the components in thirdPanel everytime secondPanel draws/repaints?Mitrewort
"I am using a null layout manager." If you look through the accepted answers of the null-layout-manager questions, I'd wager that most, if not all of them, were solved by using a layout manager. For better help sooner, post an MCVE (Minimal, Complete, Verifiable Example).Jimmyjimsonweed
Ok thanks I'll have a look at those questions.Mitrewort
W
2

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.

Wiltz answered 22/8, 2014 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.