setOpaque : Java Glossary

go to home page S words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2009-04-04 Roedy Green, Canadian Mind Products
setOpaque
JComponent.setOpaque( boolean ) is misnamed. It really should be called JComponent. wipeBackground( boolean ). When you setOpaque( true ), Swing wipes the background to the component’s background colour before calling your paintComponent method. This replaces the AWT update/ paint mechanism.
  1. You would set setOpaque( false ) if you want what is behind your component to bleed through, e.g. if you were drawing a round button.
  2. You would set setOpaque( true ) if you wanted nothing to show through — you wanted unpainted parts of your component to appear as the background colour.
  3. For efficiency, even if you had an opaque component, you might setOpaque ( false ) when your paintComponent method painted every single pixel, so there was no background to show through. There is no point in clearing the background before painting.
setOpaque Usage
setOpaque(?) call super.paintComponent
in paintComponent?
Meaning
false There is no point in doing this.
false You will paint only some of the pixels or use some transparent pixels to deliberately let what is painted underneath show through.
true You paint every pixel of the region, with no transparent pixels, and you want Swing to clear the region to the background colour for you automatically.
true You paint every pixel of the region, with no transparent pixels, and you will clear the background as needed with clearRect yourself in your paintComponent method.
The symptom of failing to call setOpaque( true ) is flicker, where the image gets repeatedly repainted to the background colour. The symptom of calling setOpaque( true ), when you should not are artifacts — little bits of left over painting from other work in where your component should be. These are areas you did not paint, which should have been background.

If in your TableCellRenderer you create your own JLabels to return, rather than using the one from DefaultTableCellRenderer, make sure you call JLabel.setOpaque( true ) or else your JLabel.setBackground will be ignored.

Sometimes the results you get from setOpaque and calliing super. paintComponent defy logic. Just try all possibilities and take the one that works.

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/setopaque.html J:\mindprod\jgloss\setopaque.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.106]
You are visitor number 11.