I have a set of JMenuItems in JPanel at the same level as JLayeredPane and an emulated cursor written in JPanel at a higher level. When menu items are redrawn, they draw over the emulated cursor (without causing the cursor layer to redraw). Interestingly, if I replaced JButtons or JLabels for menu items, the cursor will be correctly colored every time the menu items are redrawn.
How can I guarantee that redrawing menu items will redraw the affected areas of higher layers without directly calling repaint () on the layered panel? The described situation is somewhat simplified from reality: menu items can be deeply embedded in the child layer of the layered panel, and they should not know about the multilevel panel at all.
Here is a snippet of pseudo code illustrating what I described:
public void initGui(Dimension size) { JLayeredPane layeredPane = new JLayeredPane(); layeredPane.setSize(size); menuPanel = new JPanel(); menuPanel.setSize(size); layeredPane.add(menuPanel, BOTTOM_LAYER); JPanel cursorPanel = new CursorPanel(); cursorPanel.setSize(size); layeredPane.add(cursorPanel, TOP_LAYER); } public void showMenu(Component[] menuItems) { JPanel menu = new JPanel(); for (Component c: menuItems) menu.add(c); menuPanel.add(menu); }
Aaron Novstrup
source share