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
| public class ColorListCellRenderer extends DefaultListCellRenderer {
// Le composant qui affiche une case de la liste lorsque c'est une couleur
static class ColorRenderer extends JComponent {
public void paintComponent(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(this.getForeground());
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
}
ColorRenderer cr = new ColorRenderer();
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if(value instanceof Color) {
// la liste contient un objet de type Color
// on renvoie notre composant maison ColorRenderer
if(isSelected)
cr.setBackground(Color.BLUE);
else
cr.setBackground(Color.WHITE);
cr.setForeground((Color)value);
return cr;
}
else
// la liste contient un objet de type inconnu
// on renvoie le composant par défaut
return super.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
} |
Partager