Add a cell renderer on the eye color JList view component that display the color...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 13:38:48 +0000 (14:38 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 13:38:48 +0000 (14:38 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/EyeView.java

index 2f892e4b99e47239078137243c3554caaa181363..99b5d0b680847537199c0747262398175aa553c4 100644 (file)
@@ -1,4 +1,5 @@
 import javax.swing.*;
+import java.awt.*;
 import java.util.Arrays;
 
 public class EyeView extends JPanel {
@@ -17,6 +18,7 @@ public class EyeView extends JPanel {
         this.colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         this.colorsList.setLayoutOrientation(JList.VERTICAL);
         this.colorsList.setSelectedIndex(Arrays.asList(this.getEyeObj().getColorsList()).indexOf(this.getEyeObj().getStrColor()));
+        this.colorsList.setCellRenderer(new EyeCellRenderer());
         add(label);
         add(colorsList);
     }
@@ -38,4 +40,17 @@ public class EyeView extends JPanel {
     public JList getColorsList() {
         return colorsList;
     }
+
+    private static class EyeCellRenderer extends DefaultListCellRenderer {
+        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+            Eye currentEye = new Eye((String) list.getModel().getElementAt(index));
+            c.setBackground(currentEye.getColor());
+            c.setForeground(Color.WHITE);
+            if (isSelected) {
+                setBackground(getBackground().darker());
+            }
+            return c;
+        }
+    }
 }