public Dimension getListPreferredSize(List l) {
int width = 0;
int height = 0;
int selectedHeight;
int selectedWidth;
ListModel model = l.getModel();
int numOfcomponents = model.getSize();
Object prototype = l.getRenderingPrototype();
if(prototype != null) {
ListCellRenderer renderer = l.getRenderer();
Component cmp = renderer.getListCellRendererComponent(l, prototype, 0, false);
height = cmp.getPreferredSize().getHeight();
width = cmp.getPreferredSize().getWidth();
cmp = renderer.getListCellRendererComponent(l, prototype, 0, true);
selectedHeight = Math.max(height, cmp.getPreferredSize().getHeight());
selectedWidth = Math.max(width, cmp.getPreferredSize().getWidth());
} else {
int hightCalcComponents = Math.min(5, numOfcomponents);
ListCellRenderer renderer = l.getRenderer();
for (int i = 0; i < hightCalcComponents; i++) {
Object value = model.getItemAt(i);
Component cmp = renderer.getListCellRendererComponent(l, value, i, false);
height = Math.max(height, cmp.getPreferredSize().getHeight());
width = Math.max(width, cmp.getPreferredSize().getWidth());
}
selectedHeight = height;
selectedWidth = width;
if (numOfcomponents > 0) {
Object value = model.getItemAt(0);
Component cmp = renderer.getListCellRendererComponent(l, value, 0, true);
selectedHeight = Math.max(height, cmp.getPreferredSize().getHeight());
selectedWidth = Math.max(width, cmp.getPreferredSize().getWidth());
}