ComboBoxColorRenderer.java
1    import javax.swing.*;
2    import java.awt.*;
3    
4    class ComboBoxColorRenderer extends DefaultListCellRenderer{
5    
6        private ListCellRenderer defaultRenderer;
7        private Melody calcMelody;
8    
9        public ComboBoxColorRenderer(ListCellRenderer defaultRenderer, Melody calcMelody){
10           this.defaultRenderer = defaultRenderer;
11           setCalcMelody(calcMelody);
12       }
13   
14       @Override
15       public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
16           Component c = defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
17           if(c instanceof JLabel){
18               if(getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName((String)value)) != -1){
19                   c.setBackground(new Color(0, 113, 47, 69));
20                   c.setForeground(Main.keyNoteColors[getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName((String)value))]); // TODO: find way to draw a black border around the base note Strings
21               } else {
22                   c.setBackground(new Color(0,0,0,0));
23               }
24           } else {
25               c.setBackground(Color.red);
26               c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
27           }
28           return c;
29       }
30   
31   
32       // Getters & Setters
33   
34       public Melody getCalcMelody(){
35           return this.calcMelody;
36       }
37   
38       public void setCalcMelody(Melody calcMelody){
39           this.calcMelody = calcMelody;
40       }
41   }
42