Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel said to be opaque what does that mean?

In Swing JPanel said to be opaque, what does that mean?

Also how JPanel VS JComponent compared in relation to opaque?

Please explain if possible in simple terms, hence i am not very experienced in GUI programming.

Thanks in advance for your help

like image 289
skystar7 Avatar asked Dec 11 '10 11:12

skystar7


People also ask

What is opaque in Java?

The term opaque has different meanings in Java 2D and in Swing. In Java 2D opacity is a rendering concept. It is a combination of an alpha value and the Composite mode. It is a degree to which the pixel colours being drawn should be blended with pixel values already present.

What is the JPanel?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

Is JPanel a window?

JPanel class works as a container to host components. JFrame is generally used as a window for hosting stand-alone applications, like an alert window or notification window. JFrame has different inner frames for performing different purposes. In the case of JPanel, one pane can hold multiple operations.


1 Answers

Opaque has a very specific meaning in Swing - it means that the component fully paints the full area within its bounds (see the setOpaque javadoc)

This is used primarily to determine whether it is necessary to repaint components behind the current component.

  • If isOpaque is true, it is unnecessary to repaint anything behind the component (since it would just be overwritten), hence such background drawing may be omitted as an optimisation.
  • If isOpaque is false, then it indicates that the component is implementing some transparency effects - e.g. drawing a component that has a semi-transparent window in the middle, or drawing a non-rectangular component

If you are creating your own JComponent and setOpaque to true but do not honour the contract (i.e. you do not draw the full area within the bounds despite claiming to be opaque) then you may get unexpected results due to the background not being redrawn.

like image 79
mikera Avatar answered Sep 28 '22 23:09

mikera