JMenuItems painting over higher components in JLayeredPane

醉酒当歌 提交于 2019-12-04 11:59:47

JComponent has a package-private alwaysOnTop() method that the Swing painting system uses to determine whether repainting a component may necessitate repainting other components. By default this method returns false, but JMenuItem overrides it to return true unless the menu item appears in a JInternalFrame. As a consequence, components that appear above a JMenuItem will not be repainted when the menu item is repainted (except when the menu item is in an internal frame).

Since alwaysOnTop() is package-private, it cannot be overridden in a custom component. It seems that the only solutions are to

  • use a different component (e.g. JButton, JLabel)
  • place the menu in an internal frame
  • make the components transparent (i.e. setOpaque(false))

I ended up using the latter solution. Since I didn't actually want transparent menu items I wrapped the paint code with calls to set/clear the opaque property:

@Override
protected void paintComponent(Graphics g)
{ 
   // paint the component as opaque
   setOpaque(true);
   super.paintComponent(g);
   setOpaque(false);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!