<Swing Dev> Fake-Opaque Swing Components

Jasper Potts Jasper.Potts at Sun.COM
Mon Nov 12 18:53:41 UTC 2007


I have a problem with Nimbus that is common with other modern look  
and feels which we need to fix. The problem is how do setBackgound()  
and setForeground() map to how the component looks.
Because of the some components having rounded corners and all  
components having 2px spacing for painting the focus glow. You end up  
with this background area around the component (in red above). The  
area is not really of color Component.getBackgound() as that has  
another meaning which is the background of the actual component  
content such as the text field background(in green above).

The obvious solution to the problem is to make the component non- 
opaque. Which means the red area would be transparent. The issue with  
this is it the performance for opaque components is much worse so for  
components like a text field which needs to be very responsive as you  
type this could be a issue.

Proposed Solution
So the proposed solution is for the component to be opaque and the  
outer background area to be painted as before but be the parent  
components getBackgound() color. This seems the right answer but  
means a change at a low level. So what I propose is adding a  
UIManager property to turn this on and changing the ComponentUI.update 
() method from:

public abstract class ComponentUI {
...
   public void update(Graphics g, JComponent c) {
	if (c.isOpaque()) {
	    g.setColor(c.getBackground());
	    g.fillRect(0, 0, c.getWidth(),c.getHeight());
	}
	paint(g, c);
   }
...
}

to:

public abstract class ComponentUI {
...
   public void update(Graphics g, JComponent c) {
	if (c.isOpaque()) {
             if (UIManager.getBoolean 
("Opaque.useParentBackgroundColor") &&
                 c.getParent() != null){
                 g.setColor(c.getParent().getBackground());
             } else {
	            g.setColor(c.getBackground());
             }
	    g.fillRect(0, 0, c.getWidth(),c.getHeight());
	}
	paint(g, c);
   }
...
}

I am open for better suggestions for the UIManager key name but that  
is the basic idea.

So what do you think??????

Thanks

Jasper

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20071112/0212f64b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: component-colors.png
Type: image/png
Size: 8446 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20071112/0212f64b/component-colors.png>


More information about the swing-dev mailing list