JDK-4289100 : JComboBox Needs a setFixedCellHeight Method
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.2.2,1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-11-08
  • Updated: 2022-03-23
  • Resolved: 2001-06-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
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.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin-beta FIXED IN: merlin-beta INTEGRATED IN: merlin-beta
14-06-2004

PUBLIC COMMENTS Fixed for merlin beta
10-06-2004

EVALUATION This is a performance problem and has been addressed by adding some new API to JComboBox: get/setPrototypeDisplayValue(). This API allows the UI to calculated the display size using this prototype value rather than iterated over a large list of items. See also 4231298. This will be added for merlin beta. mark.davidson@Eng 2000-04-06
06-04-2000