Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel: Code for background-painting  RSS feed

 
Sven Farrenkopf
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched Swing-Sources but somehow I can't find where JLabel checks for an opaque Background and paints it. The closest thing I found was in BasicLabelUI:



But there is no opaque-check and no background-drawing.

(I need the code for debugging my own. JLabel seems to give different heights when used in a JTable - since "normal" background-painting works fine in swing and it definetly needs the height to paint it, I was looking for the code)

Sven
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must confess I do not really understand what your question or problem is. Perhaps you could rephrase it please?
If it helps, you can always call JLabel#setOpaque(true) (default is false for JLabel)
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A way to paint a background on a JLabel without calling setOpaque(true) is to set an Icon that returns 0 for its width and height and paints the entire label.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, nasty hacks!
 
Sven Farrenkopf
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, though my actual problem is solved, I want to rephrase my question:

Since Swing sources are open, I was looking for the line of code that actually painted the background of a JLabel. I wasn't looking for hacks, since (in my case) setBackground() worked just fine. But when I wanted to paint my own (multi-color) background in a subclass of JLabel, the sizing didn't work properyly. So I was looking for an explanation by comparing both sources. I still haven't found that particular line of code, though I found the explanation of what I did wrong in a different code-passage of Swing. (I used column- and row-spanning in my table which was the origin of my problem - the Rectangle had a bug in its calculation.)

But thanks anyway

Sven

 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Farrenkopf wrote:Since Swing sources are open, I was looking for the line of code that actually painted the background of a JLabel.

Check out the source of javax.swing.plaf.basic.BasicLabelUI.
 
Sven Farrenkopf
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Check out the source of javax.swing.plaf.basic.BasicLabelUI.


Have you read my first posting? I already checked - couldn't find it.

Sven
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I wanted to paint my own (multi-color) background in a subclass of JLabel, the sizing didn't work properyly.


Painting a background would have nothing to do with that.

I believe the background is painted in ComponentUI.update(...)



If you look in the BasicLabelUI you will see code like:



If you look in the BasicPanelUI you will see code like:



So all components share the same background painting code (unless it is overridden somewhere) and the opaque property controls the painting of the background.
 
Sven Farrenkopf
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote: I believe the background is painted in ComponentUI.update(...)


Now THAT'S what I was looking for. Thanks!

Sven
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!