JDK-6404438 : REGRESSION: JFrame and GridbagLayout ArrayIndexOutOfBoundsException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-27
  • Updated: 2011-02-16
  • Resolved: 2006-03-28
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When a JPanel  with GridbagLayout as its LayoutManager  is added to a JFrame, or A JFrames LayoutManager is set to GridBagLayout an ArrayIndexOutOfBoundsException is thrown.

I have not checked this with any other containers.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run source code example

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
JFrame will show
ACTUAL -
ArrayIndexOutOfBoundsException is thrown

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.awt.GridBagLayout.GetLayoutInfo(Unknown Source)
	at java.awt.GridBagLayout.getLayoutInfo(Unknown Source)
	at java.awt.GridBagLayout.preferredLayoutSize(Unknown Source)
	at java.awt.Container.preferredSize(Unknown Source)
	at java.awt.Container.getPreferredSize(Unknown Source)
	at javax.swing.JComponent.getPreferredSize(Unknown Source)
	at javax.swing.JRootPane$RootLayout.preferredLayoutSize(Unknown Source)
	at java.awt.Container.preferredSize(Unknown Source)
	at java.awt.Container.getPreferredSize(Unknown Source)
	at javax.swing.JComponent.getPreferredSize(Unknown Source)
	at java.awt.BorderLayout.preferredLayoutSize(Unknown Source)
	at java.awt.Container.preferredSize(Unknown Source)
	at java.awt.Container.getPreferredSize(Unknown Source)
	at java.awt.Window.pack(Unknown Source)
	at com.spectrum.genius.test.NewJFrame.<init>(NewJFrame.java:36)
	at com.spectrum.genius.test.NewJFrame.main(NewJFrame.java:23)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
#Test Case 1
import java.awt.GridBagLayout;

import javax.swing.WindowConstants;

public class NewJFrame extends javax.swing.JFrame{

    public static void main(String[] args){
        NewJFrame inst = new NewJFrame();
        inst.setVisible(true);
    }

    public NewJFrame() {
        super();
        GridBagLayout thisLayout = new GridBagLayout();
        thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.rowHeights = new int[] { 7, 7, 7, 7 };
        thisLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.columnWidths = new int[] { 7, 7, 7, 7 };
        getContentPane().setLayout(thisLayout);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        pack();
        setSize(400, 300);
    }

}

-------------------------------------------------------------------------------------------------------
#Test Case 2
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class NewJFrame extends javax.swing.JFrame{

    public static void main(String[] args){
        NewJFrame inst = new NewJFrame();
        inst.setVisible(true);
    }

    public NewJFrame() {
        super();
        GridBagLayout thisLayout = new GridBagLayout();
        thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.rowHeights = new int[] { 7, 7, 7, 7 };
        thisLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.columnWidths = new int[] { 7, 7, 7, 7 };
        JPanel p = new JPanel(thisLayout);
        setContentPane(p);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        pack();
        setSize(400, 300);
    }

}
-------------------------------------------------------------------------------------------------------
#Test Case 3
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class NewJFrame extends javax.swing.JFrame{

    public static void main(String[] args){
        NewJFrame inst = new NewJFrame();
        inst.setVisible(true);
    }

    public NewJFrame() {
        super();
        GridBagLayout thisLayout = new GridBagLayout();
        thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.rowHeights = new int[] { 7, 7, 7, 7 };
        thisLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
        thisLayout.columnWidths = new int[] { 7, 7, 7, 7 };
        JPanel p = new JPanel(thisLayout);
        getContentPane().add(p);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        pack();
        setSize(400, 300);
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use a JDialog instead of a JFrame

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

Comments
EVALUATION It doesn't depend on type of component but it depends on layout manager only. If you set some of rowWeights, rowHeights, columnWeights or columnWidths arrays length more then components in that container you get the exception on getLayoutInfo().
28-03-2006