JDK-5029286 : REGRESSION: JColorChooser.setPreviewPanel() does not show custom preview panel
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-04-08
  • Updated: 2004-12-26
  • Resolved: 2004-12-26
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

Name: rmT116609			Date: 04/08/2004


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

java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
With j2re1.4.2 JColorChooser does not show a custom preview panel, that was set with JColorChooser.setPreviewPanel(). Instead the default preview panel is removed and the space is left blank. This happens with code, that was perfectly working in 1.4.1_02 and prior versions. It happens with metal L&F and windows L&F. It is still possible to use the default preview panel.

I already tested this with 1.5.0 beta1 before and it seems to work if you set a JButton as the new previewPanel but fails with all other JComponents.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the given code in 1.4.1_02 and 1.4.2. The preview panel shows up with 1.4.1_02 but not with 1.4.2.

The code just constructs a JColorChooser within a JFrame and sets a custom preview panel, which is a JLabel in this case.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect the custom preview panel to appear on screen.
ACTUAL -
Custom preview panel does not show up, default preview panel is removed and space left blank.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error messages.

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class JColorChooserTest extends javax.swing.JFrame {
    
    /** Creates new form JcolorChooserTest */
    public JColorChooserTest() {
        initComponents();

        PreviewPanel previewPanel = new PreviewPanel();
        jColorChooser1.setPreviewPanel(previewPanel);
        jColorChooser1.getSelectionModel().addChangeListener(previewPanel);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jColorChooser1 = new javax.swing.JColorChooser();

        getContentPane().setLayout(new java.awt.FlowLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        getContentPane().add(jColorChooser1);

        pack();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new JColorChooserTest().show();
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JColorChooser jColorChooser1;
    // End of variables declaration
    
    class PreviewPanel extends JLabel implements javax.swing.event.ChangeListener{
        
        public PreviewPanel() {
            super("This is the selected color!");
        }
        
        public void stateChanged(javax.swing.event.ChangeEvent e) {
            Color newColor = jColorChooser1.getColor();
            setForeground(newColor);
        }
        
    }
}
---------- END SOURCE ----------

Release Regression From : 1.4.1
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 189871) 
======================================================================

Comments
EVALUATION Name: ibR10256 Date: 04/14/2004 Seems the problem is caused by the fix for 4759306 in BasicColorChooserUI.installPreviewPanel: layoutSize = new BorderLayout().minimumLayoutSize(previewPanel); if ((previewPanelHolder != null) && (chooser != null) && (layoutSize.getWidth() + layoutSize.getHeight() == 0)) { chooser.remove(previewPanelHolder); return; } Here minimumLayoutSize returns (0,0) for JPanel, JLabel, and non-zero values for JButton, and so previewPanelHolder is removed from the chooser before previewPanel is added to it. ###@###.### 2004-04-14 ====================================================================== Name: ibR10256 Date: 04/23/2004 The reason because minimumLayoutSize in the above code returns 0,0 in most of the cases is that this method requires the target component to be added to the layout to calculate the required space. But even modifying this code as: BorderLayout layout = new BorderLayout(); layout.addLayoutComponent(previewPanel, BorderLayout.SOUTH); Dimension layoutSize = layout.minimumLayoutSize(previewPanel); doesn't give the desired result, because in this case the returned minimum size of the empty JPanel is (10,10): which is not (0,0). ###@###.### 2004-04-23 ====================================================================== Name: ibR10256 Date: 04/23/2004 The fix for 4759306 suggested users to pass an empty JPanel to setPreviewPanel() if they wanted to remove the default preview panel along with previewPanelHolder. This behaviour is not specified by the javadoc, is hardly implementable in a non error prone way, and I think, is not reasonable. I suggest to change it in the following way: - setting a custom panel shouldn't lead to the checks of the sizes of the panel submitted by the user, - complete removal of the preview panel should be done upon: setPreviewPanel(null) The default preview panel still can be restored this way: setPreviewPanel(ColorChooserComponentFactory.getPreviewPanel()) - after removal of the preview panel it should be possible to restore the default panel or set a custom panel (now it's not possible) ###@###.### 2004-04-23 ====================================================================== This bug is caused by the same reason as #6199676 closed as duplicate ###@###.### 2004-12-26 16:46:02 GMT
26-12-2004