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

jComboBox.removeAllItems() disables item reselection after providing new items

XMLWordPrintable



      Name: rmT116609 Date: 08/22/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      java (TM) 2 runtime environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      and:

      java version "1.4.1-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
      Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows XP [Version 5.1.2600], Windows 2000

      A DESCRIPTION OF THE PROBLEM :
      After using jComboBox.removeAllItems() and refilling the combo with different items, a selection cannot be made.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      import java.lang.*;
      import java.awt.event.*;
      import java.util.*;

      public class Frame1 extends JFrame
      {
        BorderLayout borderLayout1 = new BorderLayout();
        JPanel jPanel1 = new JPanel();
        JComboBox jComboBox1;

        public Frame1 ()
        {
          try
          {
            jbInit();
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }

        }

        private void jbInit() throws Exception
        {
          //init window view
          this.getContentPane().setLayout(borderLayout1);
          this.setSize(new Dimension(400, 300));

          jPanel1.setBackground(new Color(228, 220, 236));
          jPanel1.setLayout(null);

          //init jComboBox1
          Vector r = new Vector();
          r.add(new String (" aaa "));
          r.add(new String (" bbb "));
          r.add(new String (" ccc "));
          r.add(new String (" ddd "));
          jComboBox1 = new JComboBox(r);
          jComboBox1.setBounds(new Rectangle(128, 61, 118, 26));

          jComboBox1.addItemListener(new ItemListener(){
              public void itemStateChanged(ItemEvent e) {
                  if (e.getStateChange() == ItemEvent.SELECTED) {
                      JComboBox cb = (JComboBox)e.getSource();
                      String name = (String)cb.getSelectedItem();
                      //create new elements for jcombo-box on item's selection
                      Vector r = new Vector();
                      r.add(new String( " eee "));
                      r.add(new String( " fff "));
                      r.add(new String( " ggg "));
                      r.add(new String( " hhh "));
                      //!!!!this is the bug - if you don't use removeAllItems()
                      //you will be able to choose any item from the combo box
                      jComboBox1.removeAllItems();

                      for(int i = 0; i < r.size(); i++){
                          jComboBox1.insertItemAt(r.get(i), i);
                     }
                   }//end if
              }
           });

          this.addWindowListener(new WindowAdapter(){
            public void windowClosing (WindowEvent evt) {
              System.exit(0);
            }
          });

          jPanel1.add(jComboBox1,null);
          this.getContentPane().add(jPanel1, BorderLayout.CENTER);
          this.show();
        }


        public static void main(String[] args) {
          Frame1 w = new Frame1();

        }

      }
      ---------- END SOURCE ----------
      (Review ID: 163475)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: