Add a cell renderer on the eye color JList view component that display the color...
[Persons_Comparator.git] / src / EyeView.java
CommitLineData
089fcbfc 1import javax.swing.*;
b05df284 2import java.awt.*;
3d9fdaf4 3import java.util.Arrays;
089fcbfc 4
d119e60d 5public class EyeView extends JPanel {
6977e614 6 private Eye eyeObj;
883508ca 7 private JLabel label;
3d9fdaf4 8 private JList colorsList;
6977e614 9
b974e749
JB
10 /**
11 * @param eyeObj
12 */
13 EyeView(Eye eyeObj) {
14 setEyeObj(eyeObj);
883508ca
JB
15 this.label = new JLabel();
16 this.label.setText("Eyes color");
3d9fdaf4
JB
17 this.colorsList = new JList<>(this.getEyeObj().getColorsList());
18 this.colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
19 this.colorsList.setLayoutOrientation(JList.VERTICAL);
20 this.colorsList.setSelectedIndex(Arrays.asList(this.getEyeObj().getColorsList()).indexOf(this.getEyeObj().getStrColor()));
b05df284 21 this.colorsList.setCellRenderer(new EyeCellRenderer());
883508ca
JB
22 add(label);
23 add(colorsList);
089fcbfc
JB
24 }
25
ac6c3ea2 26 /**
ac6c3ea2
JB
27 * @return
28 */
6977e614
JB
29 public Eye getEyeObj() {
30 return eyeObj;
31 }
32
ac6c3ea2 33 /**
ac6c3ea2
JB
34 * @param eyeObj
35 */
6977e614
JB
36 public void setEyeObj(Eye eyeObj) {
37 this.eyeObj = eyeObj;
38 }
9b3bfcdd
JB
39
40 public JList getColorsList() {
41 return colorsList;
42 }
b05df284
JB
43
44 private static class EyeCellRenderer extends DefaultListCellRenderer {
45 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
46 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
47 Eye currentEye = new Eye((String) list.getModel().getElementAt(index));
48 c.setBackground(currentEye.getColor());
49 c.setForeground(Color.WHITE);
50 if (isSelected) {
51 setBackground(getBackground().darker());
52 }
53 return c;
54 }
55 }
6977e614 56}