Strange alpha channel behavior in jscroll pane
Hi,
My first post here.
I have been strugling for a while with a problem with alpha channel opacity and scrollpane. I basically have a JScrollpane that contains a JPanel with JLabels. The JLabels have a mouse action that changes their background to higlighted by using a color with an alpha channel.
When a mouse is over a JLabel we call:
public void mouseOver(){
this.setForeground(Color.WHITE);
this.setBackground(new Color(100,100,100,100));
this.setOpaque(true);
}
This works but with some strange rendering problems. Sometimes the "background" shows other parts of the screen insted of the correct background. This is kind of hard to explain so I took a screen shot that elaborates this.
The correct behavior:
http://www.rulli.org/~mravimo/seka/correct.jpg
The wrong bahavior:
http://www.rulli.org/~mravimo/seka/wrong.jpg
[911 byte] By [
ra1p3a] at [2007-11-26 17:31:08]

# 1
I think there seems to some abuse of the 'opaque' setting in Java. Opaque, literally, of course, describes something through which nothing can be seen (if you can see through it, it's either translucent or transparent). That means alpha 0 is transparent, alpha 255 (for an 8-bit channel) is opaque, and anything else is translucent.
ComponentUI uses 'opaque' to determine whether to paint the component background. This is where the semantics are broken: it should really query the background color to see if it's non-null and its alpha is non-zero, thus painting translucent colors as well as opaque. I think there is a good case for this being a Java bug if it's not already - though I'd anticipate the counter-argument being that it will raise issues with backward-compatibility ("if it's been broken long enough, don't fix it").
The problem you're seeing, I suspect, is due to the same flag being used (semantically correctly) to help optimise the paint process (if something really is opaque then there's no point rendering underneath it).
Unfortunately, fixing ComponentUI is a non starter since it can only be fixed by extension (multiply the number of components by the number of L&Fs and you're on your way).
As a workaround you can extend JLabel and override paintComponent(); or plug in a specific label UI which fixed the ComponentUI problem; in both cases leaving the opaque setting at false.
# 2
>> As a workaround you can extend JLabel and override paintComponent(); or plug in a specific label UI which fixed the ComponentUI problem; in both cases leaving the opaque setting at false.
I am sorry. I do not understand what you mean. I don't understand how oveririding the paintComponent() wuold help the situation. Why would not the same problem still exist? Also I do not understand what should I put to the paintComponent() method, just create the content from scratch?
# 3
If you make the component opaque, then the parent background is not painted and your transparent color shows garbage from the parent.
If you keep the component non-opaque, then the background doesn't get painted by the default painting code so you see the background color of the parent.
So you need to keep the component non-opaque and paint the background yourself:
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent( g );
}
# 4
Thanks that worked out fine! Here is the simplified code that solves the problem.
public class EntryLabel extends JLabel {
private Color selected = new Color(100,100,100,100);
private Color normal = new Color(0,0,0,0);
private Color backgroundColor;
public EntryLabel(String text) {
super(text);
this.setOpaque(false);
this.backgroundColor = this.normal;
this.setBackground(null);
}
public void mouseOver(){
this.backgroundColor = this.selected;
}
public void mouseOut() {
this.backgroundColor = this.normal;
}
public void paintComponent(Graphics g){
g.setColor(this.backgroundColor);
g.fillRect(0,0,getSize().width,getSize().height);
super.paintComponent(g);
}
}
EDIT:
BTW. How do I make the code part of the message to use those formattings ?
Message was edited by:
ra1p3
# 5
How do I make the code part of the message to use those formattings ?Use code tags - bonk on the "code" button above the text area when posting :o)