Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between setOpaque() and setVisible()

 
Ken Austin
Ranch Hand
Posts: 39
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning, all. I am still working through a beginner textbook, and I have a quick question for you...

Can someone explain the difference between setVisible() and setOpaque()?

The API docs say this about setVisible()
Makes the component visible or invisible. Overrides Component.setVisible.


The API docs say this about setOpaque()
If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.


Why is there a need for setOpaque()?

Ken

(edited for grammar)
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- (J)Components#setVisible() is method for to show or hide (J)Component in the AWT / Swing GUI

- (J)Components#setOpaque() is method for to set transparency for (J)Component,

1. for example JLabel, JViewport, GlassPane are opaque

2. you can pass AlphaComposite (new Color (int, int, int, AlphaComposite)) as transparency parameter for (J)Components

3. notice Transparency by using setOpaque(boolean b) or AlphaComposite is diferrent with compare with Java Translucency

- there are a few code example on this Forum, Oracle tutorial, www.java2s.com (notice examples for Graphics(2D) are in most cases outdated, have to change method paint() to the paintComponent or paintChildren)
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a component is opaque, it's obligated to paint its background completely. Opacity for a given component in Swing indicates whether or not the content behind a given component is visible to the user. E.g. JButton is typically opaque because it completely "hides" whatever happens behind it. On the other hand, if you want to have a rounded button of your own, you need to make sure it is not opaque because there are things behind its bounds that should be visible. I think all Swing components are opaque by default except for JLabel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic