1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
class ColorPreviewComboBoxRenderer extends JLabel implements ListCellRenderer {
Color activeCellBackground = new Color(230, 230, 230);
Color inactiveCellBackground = Color.WHITE;
Color activeCellForeground = Color.BLACK;
Color inactiveCellForeground = Color.BLACK;
public ColorPreviewComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(LEFT);
setVerticalAlignment(CENTER);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
int selectedIndex = ((Integer)value).intValue();
if (isSelected) {
setBackground(activeCellBackground);
setForeground(activeCellForeground);
} else {
setBackground(inactiveCellBackground);
setForeground(inactiveCellForeground);
}
ColorPreview icon = iconColorList[selectedIndex];
String colorStr = strColorList[selectedIndex];
setIcon(icon);
setText(colorStr);
setFont(new Font("Helvetica", Font.TRUETYPE_FONT, 12));
setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 2));
return this;
}
} |
Partager