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

Problem with JComboBox while using dynamic data model

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_16"
      Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
      Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

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

      A DESCRIPTION OF THE PROBLEM :
      I extended DefaultComboBoxModel to bind it with my existing data model (Hashtable with String[] - JComboBox list data depends on key which determines what array to get). The problem is that the size of popup (number of items in JComboBox popup) is counted when it become visible at first time. If number of items is become lesser then everything will be normal. But if number become greater then items in popup won't display.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Change first combobox value on "fruits"
      2) Open second combobox
      3) Change first combobox value on "drugs"
      4) Open second combobox

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Second combobox popup contains 3 items:
      euthanasipame,
      aspirine,
      analgine
      ACTUAL -
      Second combobox popup contains nothing.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package fcombobox;

      import java.awt.event.ActionEvent;
      import java.util.Hashtable;
      import javax.swing.DefaultComboBoxModel;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import sun.awt.VerticalBagLayout;

      public class Main {

          public static void main(String[] args) {
              Attribute attribute = new Attribute();
              attribute.putValues("fruits", new String[] {"apples", "peaches"});
              attribute.putValues("drugs", new String[] {"euthanasipame", "aspirine", "analgine"});
              attribute.setSelectedKey("drugs");

              JFrame mainWindow = new JFrame("FComboBox");
              mainWindow.setSize(640, 480);
              mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              mainWindow.setLayout(new VerticalBagLayout());
              mainWindow.add(new SettingKeyComboBox(attribute));
              FAttributeComboBox attributeComboBox = new FAttributeComboBox(attribute);
              mainWindow.add(attributeComboBox);
              mainWindow.setVisible(true);
              mainWindow.validate();
          }

          private static class SettingKeyComboBox extends JComboBox{

              public SettingKeyComboBox(Attribute attribute) {
                  super(attribute.getKeys());
                  this.attribute = attribute;
                  this.addActionListener(this);
              }

              @Override
              public void actionPerformed(ActionEvent e) {
                  attribute.setSelectedKey((String) this.getSelectedItem());
              }

              private Attribute attribute;
          }

          private static class Attribute {

              final public static String UNDEFINED_STRING = "";

              public Attribute() {
              }

              public void putValues(String key, String[] value) {
                  values.put(key, value);
              }

              public String getValue(int index) {
                  String[] currentValues = values.get(selectedKey);
                  return index < currentValues.length ?
                      values.get(selectedKey)[index] : UNDEFINED_STRING;
              }

              public String getSelectedValue() {
                  return selectedValue;
              }

              public void setSelectedValue(String selectedValue) {
                  this.selectedValue = selectedValue;
              }

              public void setSelectedKey(String selectedKey) {
                  if (!this.selectedKey.equals(selectedKey)) {
                      this.selectedKey = selectedKey;
                      selectedValue = values.get(selectedKey)[0];
                  }
              }

              public int getSize() {
                  return values.get(selectedKey).length;
              }

              public Object[] getKeys() {
                  return values.keySet().toArray();
              }

              private String selectedKey = "";
              private String selectedValue = "";
              private Hashtable<String, String[]> values =
                      new Hashtable<String, String[]>();
          }

          private static class FAttributeComboBox extends JComboBox {

              public FAttributeComboBox(Attribute attribute) {
                  this.setModel(new FAttributeComboBoxModel(attribute));
              }

              private class FAttributeComboBoxModel extends DefaultComboBoxModel {

                  @Override
                  public Object getElementAt(int index) {
                      return attribute.getValue(index);
                  }

                  @Override
                  public Object getSelectedItem() {
                      return attribute.getSelectedValue();
                  }

                  @Override
                  public void setSelectedItem(Object anObject) {
                      attribute.setSelectedValue((String) anObject);
                  }

                  @Override
                  public int getSize() {
                      return attribute.getSize();
                  }

                  public FAttributeComboBoxModel(Attribute attribute) {
                      this.attribute = attribute;
                  }

                  private Attribute attribute;
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The simpliest way to solve this problem is to use specificity of situation. After run we invoke some sort of "init" method which just opens and closes combobox with extended size. But it's very buggy way.

            alexp Alexander Potochkin (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: