United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6589530 HW/LW mixing code incorrectly handles insertion of components with the setComponentZOrder() method
JDK-6589530 : HW/LW mixing code incorrectly handles insertion of components with the setComponentZOrder() method

Details
Type:
Bug
Submit Date:
2007-08-06
Status:
Closed
Updated Date:
2012-03-22
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
client-libs
OS:
linux,generic,windows_xp
Sub-Component:
java.awt
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6u12,OpenJDK6,7
Fixed Versions:

Related Reports
Backport:
Backport:
Duplicate:
Duplicate:
Duplicate:
Duplicate:

Sub Tasks

Description
The Container.setComponentZOrder(Component comp, int index) method may be used to insert components into a Container. In this case the oldIndex value that gets passed to the mixOnZOrderChanging() method is -1. This actually causes an exception with the following stack:

java.lang.ArrayIndexOutOfBoundsException: No such child: -1
	at java.awt.Container.getComponent(Container.java:307)
	at java.awt.Component.mixOnZOrderChanging(Component.java:9696)
	at java.awt.Container.setComponentZOrder(Container.java:713)
The following test reproduces the problem.

import java.awt.*;

public class test {
    public static void main(String[] args) {
        Container c = new Container();
        Button b = new Button("b");
        c.setComponentZOrder(b, 0);
    }
}

                                    

Comments
EVALUATION

The effect happens because the mixOnZOrderChanging() method is not aware of such uncommon usage of the setComponentZOrder() method. To fix this, we should check the value of the oldIndex argument before passing it to the getComponent() method. If the oldIndex is equal to -1, then this is an insertion, and there's no need to call mixOnZOrderChanging(). The mixOnShowing() will correctly be invoked on the addNotify() and/or setVisible(true) invokations.
                                     
2007-08-06
SUGGESTED FIX

$ sccs diffs -C Container.java

------- Container.java -------
*** /tmp/sccs.OkhFmi    2007-08-08 11:53:20.000000000 +0400
--- Container.java      2007-08-07 17:43:01.000000000 +0400
***************
*** 713,719 ****
               
               addDelicately(comp, curParent, index);
  
!              if (!peerRecreated) {
                   comp.mixOnZOrderChanging(oldZindex, index);
               }
           }
--- 713,726 ----
               
               addDelicately(comp, curParent, index);
  
!              // If the oldZindex == -1, the component gets inserted, 
!              // rather than it changes its z-order.
!              if (!peerRecreated && oldZindex != -1) {
!                  // The new 'index' cannot be == -1.
!                  // It gets checked at the checkAdding() method.
!                  // Therefore both oldZIndex and index denote
!                  // some existing positions at this point and
!                  // this is actually a Z-order changing.
                   comp.mixOnZOrderChanging(oldZindex, index);
               }
           }
                                     
2007-08-06



Hardware and Software, Engineered to Work Together