Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4173836

RFE: setListData in JComboBox

XMLWordPrintable

    • Cause Known
    • generic
    • generic

      Name: diC59631 Date: 09/15/98


      In JList there is a setListData method to fill a JList with one command.
      In JComboBox I have to call the JComboBox constructor with a Vector as a
      parameter to fill the JComboBox in one command. If I create an empty JComboBox
      and I want to fill it later I have to use the addItem method.

      Considerations:

      1. Why not provide a setListData method also for JComboBox?
      2. Why not provide an addItem method for JList?
      3. The addItem method in JComboBox is a real performanec bottleneck when you use it.
      (Review ID: 38874)
      ======================================================================
      Suggested fix by java.net member leouser:

      A DESCRIPTION OF THE FIX :
      BUGID: 4173836 RFE: setListData in JComboBox
      FILES AFFECTED: javax.swing.JComboBox
      JComboBox enhanced with java 6 version:
      jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin

      Discusion(embeded in test case as well):
      /**
       * BUGID: 4173836 RFE: setListData in JComboBox
       * An old and simple RFE. Ive added two methods to JComboBox:
       * setListData(Object[])
       * setListData(Vector<?>)
       * which makes setting the list data as simple as JLists. Im not
       * touching the other suggestion for a addItem to JList since I think
       * that's out of scope. Maybe needs its own RFE. This one must have
       * been lost over and over again, its 6 lines of code!
       *
       * ANTI-RATIONALE:
       * 1. A subclass may have added methods of the same name.
       * 2. Can already do this with setModel(new DefaultComboBoxModel(...))//yuck...
       *
       * TESTING STRATEGY:
       * Create 2 JComboBoxes, set the data with the new methods, assert that
       * the data returned from getItemAt is in fact equal with the new data.
       *
       *
       * JComboBox enhanced with java 6 version:
       * jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
       *
       * test ran succesfully on a SUSE 7.3 Linux distribution
       *
       * Brian Harry
       * ###@###.###
       * jan 24, 2006
       */

      UNFIED DIFF:
      --- /home/nstuff/java6/jdk1.6.0/javax/swing/JComboBox.java Thu Dec 15 02:17:35 2005
      +++ /home/javarefs/javax/swing/JComboBox.java Tue Jan 24 18:50:10 2006
      @@ -265,6 +265,30 @@
           }
       
           /**
      + * Constructs a <code>ComboBoxModel</code> from an array of objects and then
      + * applies <code>setModel</code> to it.
      + *
      + * @param listData an array of Objects containing the items to display
      + * in the list
      + * @see #setModel
      + */
      + public void setListData(Object[] listData){
      + setModel(new DefaultComboBoxModel(listData));
      + }
      +
      + /**
      + * Constructs a <code>ComboBoxModel</code> from a <code>Vector</code> and then
      + * applies <code>setModel</code> to it.
      + *
      + * @param listData a <code>Vector</code> containing the items to
      + * display in the list
      + * @see #setModel
      + */
      + public void setListData(Vector<?> listData){
      + setModel(new DefaultComboBoxModel(listData));
      + }
      +
      + /**
            * Sets the data model that the <code>JComboBox</code> uses to obtain
            * the list of items.
            *


      JUnit TESTCASE :
      import javax.swing.*;
      import java.util.*;
      import junit.framework.TestCase;
      import junit.textui.TestRunner;
      import static java.lang.System.out;

      /**
       * BUGID: 4173836 RFE: setListData in JComboBox
       * An old and simple RFE. Ive added two methods to JComboBox:
       * setListData(Object[])
       * setListData(Vector<?>)
       * which makes setting the list data as simple as JLists. Im not
       * touching the other suggestion for a addItem to JList since I think
       * that's out of scope. Maybe needs its own RFE. This one must have
       * been lost over and over again, its 6 lines of code!
       *
       * ANTI-RATIONALE:
       * 1. A subclass may have added methods of the same name.
       * 2. Can already do this with setModel(new DefaultComboBoxModel(...))//yuck...
       *
       * TESTING STRATEGY:
       * Create 2 JComboBoxes, set the data with the new methods, assert that
       * the data returned from getItemAt is in fact equal with the new data.
       *
       *
       * JComboBox enhanced with java 6 version:
       * jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
       *
       * test ran succesfully on a SUSE 7.3 Linux distribution
       *
       * Brian Harry
       * ###@###.###
       * jan 24, 2006
       */
      public class JCTest4173836 extends TestCase{


          public JCTest4173836(String method){
              super(method);
          }

          public void createGUI(){
              out.println();
              out.println("Testing new methods...");
              JFrame jf = new JFrame();
              JPanel jp = new JPanel();
              jf.add(jp);
              JComboBox jcb1 = new JComboBox();
              jp.add(jcb1);
              JComboBox jcb2 = new JComboBox();
              jp.add(jcb2);
              out.println( "testing the setListData(Object[]) method...");
              Object[] data = new Object[]{ "Java", "is", "cool"};
              jcb1.setListData(data);
              for(int i = 0; i < data.length; i++){
                  Object x = jcb1.getItemAt(i);
      out.println( i + " is: " + x + ", " + data[i]);
      assertEquals(data[i], x);
              }

              Vector<Object> v = new Vector<Object>();
      for(Object x: data) v.add(x);
              out.println( "testing the setListData(Vector<?>) method...");
      jcb2.setListData(v);
              for(int i = 0; i < data.length; i++){
                  Object x = jcb2.getItemAt(i);
      out.println( i + " is: " + x + ", " + data[i]);
      assertEquals(v.get(i), x);
              }

      jf.pack();
      jf.setVisible(true);

          }


          public void testCombo(){
              Runnable run = new Runnable(){
      public void run(){
                          createGUI();
                      }

      };
              SwingUtilities.invokeLater(run);
          }


          public static void main(String ... args){
              TestCase tc = new JCTest4173836("testCombo");
      TestRunner.run(tc);
          }



      }


      FIX FOR BUG NUMBER:
      4173836

            Unassigned Unassigned
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: