JDK-6459798 : JDesktopPane,JFileChooser violate encapsulation by returning internal Dimensions
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: x86
  • Submitted: 2006-08-14
  • Updated: 2015-09-29
  • Resolved: 2015-01-21
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 8 JDK 9
8u60Fixed 9 b52Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0-rc-b94)

A DESCRIPTION OF THE PROBLEM :
JDesktopPane and JFileChooser violate encapsulation by returning internal Dimensions that determine the minimum or maximum size.

According to my test, JFileChooser returns an internal minimum Dimension, that if altered alters the minimum size of the JFileChooser.

JDesktopPane appears to do it with the maximum size.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test with assertions enabled, see which classes fail and why.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That altering the returned Dimension will not alter the internals of the class
ACTUAL -
internals are altered, or appear that way

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class TestDimensionEncapsulation implements Runnable{

    public void run(){
	runTest(new Panel());
        runTest(new Button());
        runTest(new Checkbox());
        runTest(new Canvas());
        runTest(new Choice());
        runTest(new Label());
        runTest(new Scrollbar());
        runTest(new TextArea());
        runTest(new TextField());
        runTest(new Dialog(new JFrame()));
        runTest(new Frame());
        runTest(new Window(new JFrame()));
        runTest(new FileDialog(new JFrame()));
        runTest(new List());
        runTest(new ScrollPane());
        runTest(new JFrame());
        runTest(new JDialog(new JFrame()));
        runTest(new JWindow(new JFrame()));
        runTest(new JLabel("hi"));
        runTest(new JMenu());
        runTest(new JTree());
        runTest(new JTable());
        runTest(new JMenuItem());
        runTest(new JCheckBoxMenuItem());
        runTest(new JToggleButton());
        runTest(new JSpinner());
        runTest(new JSlider());
        runTest(Box.createVerticalBox());
        runTest(Box.createHorizontalBox());
        runTest(new JTextField());
        runTest(new JTextArea());
        runTest(new JTextPane());
        runTest(new JPasswordField());
        runTest(new JFormattedTextField());
        runTest(new JEditorPane());
        runTest(new JButton());
        runTest(new JColorChooser());
        runTest(new JFileChooser());
        runTest(new JCheckBox());
        runTest(new JInternalFrame());
        runTest(new JDesktopPane());
        runTest(new javax.swing.table.JTableHeader());
        runTest(new JLayeredPane());
        runTest(new JRootPane());
        runTest(new JMenuBar());
        runTest(new JOptionPane());
        runTest(new JRadioButton());
        runTest(new JRadioButtonMenuItem());
        runTest(new JPopupMenu());
        //runTest(new JScrollBar()); --> don't test defines max and min in terms of preferred
        runTest(new JScrollPane());
        runTest(new JViewport());
        runTest(new JSplitPane());
        runTest(new JTabbedPane());
        runTest(new JToolBar());
        runTest(new JSeparator());
        runTest(new JProgressBar());
        System.out.println("These classes failed");
        for(Component failure: failures)
	    System.out.println(failure.getClass());
    }
    java.util.List<Component> failures = new java.util.ArrayList<Component>();
    public void runTest(Component c){

        try{
            test(c);
            c.setMinimumSize(new Dimension(1200,1010));
            c.setMaximumSize(new Dimension(200,2010));
            c.setPreferredSize(new Dimension(400,10201));
            test(c);
        }catch(Throwable e){
            e.printStackTrace();
	    System.out.println(c.getClass() + " Crashed test");
            failures.add(c);
        }
    }

    public void test(Component c){
        Dimension psize = c.getPreferredSize();
        psize.width += 200;
        //assert(psize != c.getPreferredSize()): "getPreferredSize returned the same Dimension!";
        assert(!psize.equals(c.getPreferredSize())): "PreferredSize altered by altering Dimension!";
	Dimension msize = c.getMaximumSize();
        msize.width += 200;
        //assert(msize != c.getMaximumSize()): "getMaximumSize returned the same Dimension!";
        assert(!msize.equals(c.getMaximumSize())): "MaximumSize altered by altering Dimension!";
	Dimension misize = c.getMinimumSize();
	misize.width +=200;
        //assert(misize != c.getMinimumSize()): "getMinimumSize returned the same Dimension!";
	assert(!misize.equals(c.getMinimumSize())): "MininumSize altered by altering Dimension";
    }

    public static void main(String ... args){
	SwingUtilities.invokeLater(new TestDimensionEncapsulation());
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
do not alter the returned dimensions

Comments
EVALUATION It is need to return defensive copy of dimensions.
16-08-2006