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

JComboBox wrong preferredSize when elements are added

    XMLWordPrintable

Details

    • kestrel
    • generic
    • generic

    Description



      Name: dbT83986 Date: 08/21/99


      When adding an elements to a live combo box,
      and if the element is wider than the widest existing element,
      and the element is the second "wider than the widest" to be
      added, the resulting new preferred size of the combo box is
      too wide.

      This is probably due to the size caching in BasicComboBoxUI,
      and particularly to line 498:
            cachedDisplaySize.setSize( tallestHeight, widestWidth );

      That should obviously be:
        cachedDisplaySize.setSize( widestWidth, tallestHeight );

      Here is a demo :

      ---

      import java.awt.*;
      import javax.swing.*;
      import java.util.*;
      import java.awt.event.*;

      class CB extends JFrame{
        public static void main(String[] a) {
          new CB();
        }

        StringBuffer sb = new StringBuffer();

        CB() {
          final Model m = new Model();
          getContentPane().add(new JComboBox(m));
          getContentPane().setLayout(new FlowLayout());
          JButton b = new JButton("Add element");
          b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      sb.append("ooooooo");
      m.add(new String(sb));
      pack();
      }
            });
          getContentPane().add(b);
          pack();
          show();
        }
      }

      class Model extends AbstractListModel implements ComboBoxModel {
        Vector v = new Vector();

        Model() {
          v.add("oooooooooooooooooooooo");
        }
        void add(Object o) {
          v.add(o);
          fireIntervalAdded(this, v.size()-1, v.size()-1);
        }
        Object selected = null;

        public void setSelectedItem(java.lang.Object o) {
          selected = o;
        }

        public Object getSelectedItem() {
          return selected;
        }

        public int getSize() {
          return v.size();
        }
        public Object getElementAt(int i) {
          return v.elementAt(i);
        }
      }
      (Review ID: 94166)
      ======================================================================

      Attachments

        Activity

          People

            mdavidsosunw Mark Davidson (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: