United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6687960 Background of component invisible with Nimbus
JDK-6687960 : Background of component invisible with Nimbus

Details
Type:
Bug
Submit Date:
2008-04-14
Status:
Resolved
Updated Date:
2011-03-01
Project Name:
JDK
Resolved Date:
2008-06-09
Component:
client-libs
OS:
generic,windows_xp
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1,6u10
Fixed Versions:
6u10 (b26)

Related Reports
Duplicate:

Sub Tasks

Description
FULL PRODUCT VERSION :
1.6.0_10

ADDITIONAL OS VERSION INFORMATION :
ver XP

A DESCRIPTION OF THE PROBLEM :
When I have e.g. a JEditorPane on a JPanel and set:
myEditorPane.setOpaque(false)
I will only see a white background, not the panel behind the editorpane.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Only create a panel with a certain background and place a JEditorPane on it. Set the editorpane to opaque(false) and have a look, if you see the panels color.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect to see the color of the panel behind.
ACTUAL -
I only see a white field.

REPRODUCIBILITY :
This bug can be reproduced always.

                                    

Comments
EVALUATION

It works with the Metal LaF, assigned to the Nimbus owner

import javax.swing.*;
import java.awt.*;

public class bug6687960 {

    private static void createGui() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.red);
        frame.add(panel);

        JEditorPane pane = new JEditorPane();
        pane.setOpaque(false);

        panel.add(pane);
        
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                bug6687960.createGui();
            }
        });
    }
}
                                     
2008-04-14
EVALUATION

This is a problem the the orginal design of Swing and how it has been confusing for years. The issue is setOpaque(false) has had a side effect in exiting LAFs which is that of hiding the background which is not really what it is ment for. It is ment to say that the component my have transparent parts and swing should paint the parent component behind it. Nimbus is the first Sun LAF to need transparency in components for rounded corners and outer glow focus. This means that in Nimbus most of the standard components are non-opaque by default. So setting them to non-opaque has no effect. JButton is nice that it has a setContentAreaPainted() method that can be used to turn off background painting but unfortunatly there has been no option for this with other Nimbus components such as JTextField. One workaround has been to set the background color to transparent like new Color(0,0,0,0). We have decided for this issue to make a color which is 100% transparent (eg, Alpha == 0) mean that the background will not be painted at all.

So the solution for this bugs requirement is:

	textField.setBorder(BorderFactory.createEmptyBorder());
	textField.setBackground(new Color(0,0,0,0));

Which will give you a textfield with no border or background.
                                     
2008-05-29



Hardware and Software, Engineered to Work Together