• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JLabel: Code for background-painting

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, nasty hacks!
 
Sven Farrenkopf
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic