149 |
* Popup list containing the combo box's menu items. |
* Popup list containing the combo box's menu items. |
150 |
*/ |
*/ |
151 |
protected ComboPopup popup; |
protected ComboPopup popup; |
152 |
|
|
153 |
protected KeyListener popupKeyListener; |
protected KeyListener popupKeyListener; |
154 |
|
|
155 |
protected MouseListener popupMouseListener; |
protected MouseListener popupMouseListener; |
156 |
|
|
157 |
protected MouseMotionListener popupMouseMotionListener; |
protected MouseMotionListener popupMouseMotionListener; |
158 |
|
|
159 |
/** |
/** |
483 |
ComboBoxEditor currentEditor = comboBox.getEditor(); |
ComboBoxEditor currentEditor = comboBox.getEditor(); |
484 |
if (currentEditor == null || currentEditor instanceof UIResource) |
if (currentEditor == null || currentEditor instanceof UIResource) |
485 |
{ |
{ |
486 |
comboBox.setEditor(createEditor()); |
currentEditor = createEditor(); |
487 |
editor = comboBox.getEditor().getEditorComponent(); |
comboBox.setEditor(currentEditor); |
488 |
} |
} |
489 |
|
editor = currentEditor.getEditorComponent(); |
490 |
|
|
491 |
comboBox.revalidate(); |
comboBox.revalidate(); |
492 |
} |
} |
855 |
} |
} |
856 |
|
|
857 |
/** |
/** |
858 |
* Returns size of the largest item in the combo box. This size will be the |
* Returns the size of the display area for the combo box. This size will be |
859 |
* size of the combo box, not including the arrowButton. |
* the size of the combo box, not including the arrowButton. |
860 |
* |
* |
861 |
* @return dimensions of the largest item in the combo box. |
* @return The size of the display area for the combo box. |
862 |
*/ |
*/ |
863 |
protected Dimension getDisplaySize() |
protected Dimension getDisplaySize() |
864 |
{ |
{ |
865 |
Object prototype = comboBox.getPrototypeDisplayValue(); |
if (!comboBox.isEditable()) |
|
if (prototype != null) |
|
|
{ |
|
|
// calculate result based on prototype |
|
|
ListCellRenderer renderer = comboBox.getRenderer(); |
|
|
Component comp = renderer.getListCellRendererComponent(listBox, |
|
|
prototype, -1, false, false); |
|
|
Dimension compSize = comp.getPreferredSize(); |
|
|
compSize.width += 2; // add 1 pixel margin around area |
|
|
compSize.height += 2; |
|
|
return compSize; |
|
|
} |
|
|
else |
|
866 |
{ |
{ |
867 |
ComboBoxModel model = comboBox.getModel(); |
Object prototype = comboBox.getPrototypeDisplayValue(); |
868 |
int numItems = model.getSize(); |
if (prototype != null) |
|
|
|
|
// if combo box doesn't have any items then simply |
|
|
// return its default size |
|
|
if (numItems == 0) |
|
869 |
{ |
{ |
870 |
displaySize = getDefaultSize(); |
// calculate result based on prototype |
871 |
return displaySize; |
ListCellRenderer renderer = comboBox.getRenderer(); |
872 |
|
Component comp = renderer.getListCellRendererComponent(listBox, |
873 |
|
prototype, -1, false, false); |
874 |
|
Dimension compSize = comp.getPreferredSize(); |
875 |
|
compSize.width += 2; // add 1 pixel margin around area |
876 |
|
compSize.height += 2; |
877 |
|
return compSize; |
878 |
} |
} |
879 |
|
else |
|
Dimension size = new Dimension(0, 0); |
|
|
|
|
|
// ComboBox's display size should be equal to the |
|
|
// size of the largest item in the combo box. |
|
|
ListCellRenderer renderer = comboBox.getRenderer(); |
|
|
|
|
|
for (int i = 0; i < numItems; i++) |
|
880 |
{ |
{ |
881 |
Object item = model.getElementAt(i); |
ComboBoxModel model = comboBox.getModel(); |
882 |
Component comp = renderer.getListCellRendererComponent(listBox, |
int numItems = model.getSize(); |
883 |
|
|
884 |
|
// if combo box doesn't have any items then simply |
885 |
|
// return its default size |
886 |
|
if (numItems == 0) |
887 |
|
{ |
888 |
|
displaySize = getDefaultSize(); |
889 |
|
return displaySize; |
890 |
|
} |
891 |
|
|
892 |
|
Dimension size = new Dimension(0, 0); |
893 |
|
|
894 |
|
// ComboBox's display size should be equal to the |
895 |
|
// size of the largest item in the combo box. |
896 |
|
ListCellRenderer renderer = comboBox.getRenderer(); |
897 |
|
|
898 |
|
for (int i = 0; i < numItems; i++) |
899 |
|
{ |
900 |
|
Object item = model.getElementAt(i); |
901 |
|
Component comp = renderer.getListCellRendererComponent(listBox, |
902 |
item, -1, false, false); |
item, -1, false, false); |
903 |
|
|
904 |
Dimension compSize = comp.getPreferredSize(); |
Dimension compSize = comp.getPreferredSize(); |
905 |
if (compSize.width + 2 > size.width) |
if (compSize.width + 2 > size.width) |
906 |
size.width = compSize.width + 2; |
size.width = compSize.width + 2; |
907 |
if (compSize.height + 2 > size.height) |
if (compSize.height + 2 > size.height) |
908 |
size.height = compSize.height + 2; |
size.height = compSize.height + 2; |
909 |
|
} |
910 |
|
displaySize = size; |
911 |
|
return displaySize; |
912 |
|
} |
913 |
|
} |
914 |
|
else // an editable combo, |
915 |
|
{ |
916 |
|
Component comp = comboBox.getEditor().getEditorComponent(); |
917 |
|
Dimension prefSize = comp.getPreferredSize(); |
918 |
|
int width = prefSize.width; |
919 |
|
int height = prefSize.height + 2; |
920 |
|
Object prototype = comboBox.getPrototypeDisplayValue(); |
921 |
|
if (prototype != null) |
922 |
|
{ |
923 |
|
FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont()); |
924 |
|
width = Math.max(width, fm.stringWidth(prototype.toString()) + 2); |
925 |
} |
} |
926 |
displaySize = size; |
displaySize = new Dimension(width, height); |
927 |
return displaySize; |
return displaySize; |
928 |
} |
} |
929 |
} |
} |