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

JComboBox Needs a setFixedCellHeight Method

XMLWordPrintable

    • beta
    • generic, x86
    • generic, windows_95, windows_nt

      JComboBox has no method that allows the programmer to set a fixed height for
      each cell, such as setFixedCellHeight for JList or setRowHeight for JTree. As a result, very large JComboBoxes -- 1000+ items -- take a long time to open. Consider the following example:

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

      public class MightyBigComboBox extends JPanel {
          ImageIcon images[];
          int boxSize = 20000;

          public MightyBigComboBox() {
            try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch (java.lang.Exception aE) {
            }
              
              String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};
              images = new ImageIcon[boxSize];
              for (int i = 0; i < boxSize; i++) {
                  images[i] = new ImageIcon(petStrings[i % 5] + ".gif");
                  images[i].setDescription(petStrings[i % 5] + i);
              }

              JComboBox petList = new JComboBox(images);
              ComboBoxRenderer renderer= new ComboBoxRenderer();
              petList.setRenderer(renderer);

              setLayout(new BorderLayout());
              add(petList, BorderLayout.NORTH);
              setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

      long before = System.currentTimeMillis();
      Dimension result = petList.getPreferredSize();
      long after = System.currentTimeMillis();
      System.out.println("Getting preferred size took " + (after - before) + " milliseconds");

          }

          public static void main(String s[]) {
              JFrame frame = new JFrame("MightyBigComboBox");
              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
              });
       
              frame.getContentPane().add(new MightyBigComboBox(),
                                         BorderLayout.CENTER);
              frame.pack();
              frame.setVisible(true);
          }

          class ComboBoxRenderer extends JLabel implements ListCellRenderer {
              public ComboBoxRenderer() {
                  setOpaque(true);
                  setHorizontalAlignment(CENTER);
                  setVerticalAlignment(CENTER);
              }

              public Component getListCellRendererComponent(
                                                 JList list,
                                                 Object value,
                                                 int index,
                                                 boolean isSelected,
                                                 boolean cellHasFocus) {
                  if (isSelected) {
                      setBackground(list.getSelectionBackground());
                      setForeground(list.getSelectionForeground());
                  } else {
                      setBackground(list.getBackground());
                      setForeground(list.getForeground());
                  }

                  ImageIcon icon = (ImageIcon)value;
                  setText(icon.getDescription());
                  setIcon(icon);
                  return this;
              }
          }
      }

      With JDK 1.2.2 on my Pentium II 450 it takes four to five seconds to get the
      preferred size. On my Ultra 1, it takes 11 to 12 seconds.

      The .gif files for the example may be found in /home/nrodin/cases/lucent/721729.

            mdavidsosunw Mark Davidson (Inactive)
            nrodinsunw Nick Rodin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: