JDK-4907674 : Box disregards setBackground() even when set Opaque(true)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1,1.4.2,5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-08-15
  • Updated: 2017-05-16
  • Resolved: 2005-09-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b53Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: jk109818			Date: 08/15/2003


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Box disregards the background property even when set to opaque, which is
inconstent with the documented behaviour of Swing (in containers such as
JPanel).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a Box, set its background and place a glue in it. Set it to be opaque.
Yet when shown the background will not match the one set.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The background should appear correctly (see sample code and workaround)
ACTUAL -
The color is the default OS background color

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Color;
import javax.swing.*;

public class BoxTest extends JFrame {
    /**
     *  Constructor for the BoxTest object
     */
    public BoxTest() {
        Box b = new Box(BoxLayout.X_AXIS);
        // Uncomment these lines for a workaround
        //JPanel b = new JPanel();
        //b.setLayout(new BoxLayout(b, BoxLayout.X_AXIS));
        b.setBackground(Color.PINK);
        b.setOpaque(true);
        b.add(Box.createGlue());
        getContentPane().add("Center", b);
        pack();
    }


    /**
     *  The main program for the BoxTest class
     *
     *@param  argv  The command line arguments
     */
    public static void main(String[] argv) {
        new BoxTest().show();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Replace:
   Box b = new Box(BoxLayout.X_AXIS);

with:
   JPanel b = new JPanel();
   b.setLayout(new BoxLayout(b, BoxLayout.X_AXIS));
(Incident Review ID: 188567) 
======================================================================

Comments
EVALUATION For compatibility reasons we decided against a UI. Instead Box (and Filler) will override paintComponent to honor the opacity. As part of this I've removed 3 unneeded methods in Filler: getMin/Pref/MaxSize. All of these were handled by JComponent and there is no need for Filler to override them.
30-08-2005

EVALUATION This is the result of Box changing from a Container to a JComponent in 1.4 and not getting a UI or overriding paintComponent. ###@###.### 2003-08-15
15-08-2003