JComboBox Custom Renderer: alignement d'un JLabel
Salut,
J'utilise un renderer pour pouvoir ajouter à une JComboBox une petite icone comme ici.
Le problème est que le JLabel, une fois centré à gauche, colle la bordure de la JList. J'aimerais pouvoir espacer cette bordure et le contenu du JLabel (icone et texte) tout en gardant l'alignement à gauche.
Voici une image du problème:
http://img211.imageshack.us/img211/2763/comboboxzd9.jpg
Et voilà le code du renderer:
Code:
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
|
class ColorPreviewComboBoxRenderer extends JLabel implements ListCellRenderer {
Color activeCellBackground = Color.WHITE;
Color inactiveCellBackground = new Color(230, 230, 230);
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 = canvasColorList[selectedIndex];
String colorStr = strColorList[selectedIndex];
setIcon(icon);
setText(colorStr);
setFont(list.getFont());
return this;
}
} |