JDK-4243707 : awt.List.select w/ param = -1 last item in list gets selected
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 1999-06-03
  • Updated: 2000-07-05
  • Resolved: 2000-07-05
Related Reports
Duplicate :  
Description
In awt.List select(int index) get unexpected behavior when the index param
is equal to -1. The last item in the list gets selected. When other invalid
values (-2) are used the call is ignored. 

The same problem occurs with the deselect(int index) method.
If you add a few items to the list and call deselect with -1 as the index it 
deselects the last item in the list.

/*
 * @(#)SelectTest.java
 * @author JTG SQE East 5/20/99
 * @version 1.0
 *
 * java.awt.Label.select method tests
 */

import java.awt.*;
import java.io.*;
import java.awt.peer.ListPeer;

/**
 * Method: java.awt.List.select
 * <p>Syntax: public void select(int index)
 * <p>Params: int index 
 * <p>Return: none
 * <p>Exception: none       
 * <p>Pupose: selects an item in the list 
 */

public class SelectTest extends Frame
{
   List l;

   SelectTest()
   {
      super("select() Test");
      l = new List();
   }

   public static void main(String[] args)
   {
      SelectTest s = new SelectTest();
     
      boolean fail = false;

      s.setSize(500, 500);
      s.setVisible(true); 
      s.add(s.l); 
      s.l.addItem("Item1"); 
      s.l.addItem("Item2");
      s.l.addItem("Item3");  
      System.out.println("index count " + s.l.getItemCount());   
      /* These should not select anything */
      
      System.out.println("test 1, param index = -1");
      s.l.select(-1); /* bug here */
           
      System.out.println("select index " + s.l.getSelectedIndex());

      if( s.l.getSelectedIndex() != -1 )    
      {
         fail = true;
         System.out.println("fail 1");
      }

      for (int i = 0; i < s.l.getItemCount(); ++i) {	
	s.l.deselect(i);
      }
      
      System.out.println("test 2, param index = -2");
      s.l.select(-2);

      System.out.println("select index " + s.l.getSelectedIndex());
      if( s.l.getSelectedIndex() != -1 )
      {
         fail = true;
         System.out.println("fail 2");
      }

      if( fail )
      {
         System.out.println("Test for List.select Failed");
         System.exit(1);
      }
      else
      {
         System.out.println("Test for List.select Passed");
         System.exit(0);
      }
   }
}


Comments
EVALUATION The problem exists in 1.1.7, 1.1.8, 1.2.2, and 1.3 on Solaris. The test runs fine on Windows. michael.martak@Eng 1999-08-04 Note: the javadoc in List.java should specify the expected behavior when the argument to select(index) is negative or out of bounds. According to "The Java Class Libraries, Second Edition, Volume 2", pages 1007-1008, an ArrayIndexOutOfBoundsException should be thrown. However, we do not throw such an exception. It would probably be best to fail silently, and not select anything in this case. We could just check to see whether the index is less than zero, or greater than getItemCount()-1, and return immediately if it is. We might want to check Choice for consistency. Sigh. Choice throws an IllegalArgumentException in this case. eric.hawkes@eng 2000-02-21
21-02-2000