Java Swing - Translucent Components causing Artifacts

旧巷老猫 提交于 2019-12-01 21:11:31

You need to tell Swing that the components are translucent, so it does also paint the background when repainting the changed components. For this, your components have to return false from the isOpaque() method (this can be achieved by setOpaque(false) when no subclass overwrites isOpaque to do something else).

With this, it should work without any manual repainting of everything, as the other answers proposed. (I already did this once.)

Edit: The reason for this is that Swing uses an optimized repainting-algorithm, repainting only these components which really need repainting (for example, a JTextField after some input) or components in front of such ones, as long as they are opaque. When a component needing repainting is not opaque (= filling its whole space with non-translucent color), also repainting of the components behind them is necessary.

myComponent.revalidate();
myComponent.repaint();
Not sure if it "force" the redraw, but it ask it to do it when possible.

Try JFrame.repaint(). That should work. If you want custom designing of your JFrame then you will have to override paint() method of JFrame. Additionally, if you want each component to be custom designed you will have to override the paint() method of each of your GUI component. Couple of things to note:

  1. You do not have to call paint() for any GUI component. You only need to call repaint().
  2. In case you do the overriding of the other GUI components [JButton, JTextField...], you do not have to call their repaint() method. The parent Container's [JFrame, JPanel...] repaint() will do the job.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!