3

My question is whether the use of setOpaque(false), which I've come to use with JPanel to layout UI in Swing, impacts painting performance more than keeping everything opaque (where isOpaque() returns true).

I'm not very familiar with how Swing renders UI but I would guess that if a JComponent is not opaque it would be harder to render what's behind the component then to simply paint an opaque background on the component.

Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52

1 Answers1

2

Yes it does add extra work when painting but I doubt you would have to worry about it.

Basically, whenever you repaint a component that is transparent you need to go up the chain to find a parent that is not opaque and then paint that component first, before painting the child component.

So the bottom line is don't worry about it. If you have a reason to use transparency then use it. If you don't have a reason then you shouldn't be using it.

You may want to check out Background With Transparency. It goes into a little more detail on what the opaque property means and how it affects painting and the problem you will have if you do use a transparent background.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • It basically comes down to laziness on my part of whether to inscribe the same background colour on JPanel used as containers as the ones it contains or just set all containers as transparent. Good to know I should stick more to keeping stuff opaque when possible. – Sammy Guergachi Aug 01 '14 at 17:30
  • @SammyGuergachi `or just set all containers as transparent.`, I don't see a big problem with this. I would only worry when you have lots of animation and the panel is repainting itself many times a second. Normally only a single component is repainted at a time. That is while you type text into a text field, only the text field repaints to even if your panel is transparent it doesn't matter. – camickr Aug 01 '14 at 17:54
  • Well in my instance I have the constraint of very slow computers needing to run this as well as the more specific constraint that I have hundreds of JPanels in a JScrollPane and I want to keep everything as smooth as possible to allow for good scrolling. I imagine scrolling in a JScrollPane needs to repaint children components right? – Sammy Guergachi Aug 01 '14 at 18:01
  • @SammyGuergachi, `I imagine scrolling in a JScrollPane needs to repaint children panels right?` No. Again, try it before assuming a problem. – camickr Aug 01 '14 at 18:05