From: Jérôme Benoit Date: Fri, 11 Jan 2019 14:54:20 +0000 (+0100) Subject: Change the EyeView for a JComboList, store the color string inside the Eye class... X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d75d0a00d45e0f62b494a7282a5902e63df32b1a;p=Persons_Comparator.git Change the EyeView for a JComboList, store the color string inside the Eye class and choose the color string in the view. Signed-off-by: Jérôme Benoit --- diff --git a/src/Eye.java b/src/Eye.java index bf83b8a..6b71f35 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -1,17 +1,34 @@ import java.awt.Color; +import java.util.Arrays; public class Eye implements Comparable { + private String strColor; private Color color; - private String[] colorsList = {"black", "green", "blue", "brown"}; + private String[] colorsList = {"black", "blue", "brown", "green"}; Eye() { + sortColorList(); } /** * @param color */ Eye(String color) { + setStrColor(color); setColor(color); + sortColorList(); + } + + public String getStrColor() { + return strColor; + } + + public void setStrColor(String strColor) { + if (validateColor(strColor)) { + this.strColor = strColor; + } else { + throw new IllegalArgumentException("Color must be" + new Eye().colorListToString()); + } } public String[] getColorsList() { @@ -36,7 +53,7 @@ public class Eye implements Comparable { this.color = Color.getColor(color); } else { - throw new IllegalArgumentException("Color must be" + new Eye().toString()); + throw new IllegalArgumentException("Color must be" + new Eye().colorListToString()); } } @@ -53,6 +70,10 @@ public class Eye implements Comparable { return false; } + private void sortColorList() { + Arrays.sort(this.colorsList); + } + /** * @param eye * @return @@ -67,8 +88,7 @@ public class Eye implements Comparable { return distance.intValue(); } - @Override - public String toString() { + private String colorListToString() { StringBuilder stringBuilder = new StringBuilder(); for (String c : colorsList) { stringBuilder.append(" "); diff --git a/src/EyeView.java b/src/EyeView.java index bf047c7..f976a4e 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -3,7 +3,7 @@ import javax.swing.*; public class EyeView extends JComponent { private Eye eyeObj; private JLabel label; - private JList colorsList; + private JComboBox colorsList; /** * @param eyeObj @@ -12,9 +12,9 @@ public class EyeView extends JComponent { setEyeObj(eyeObj); this.label = new JLabel(); this.label.setText("Eyes color"); - this.colorsList = new JList(this.getEyeObj().getColorsList()); - this.colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - this.colorsList.setLayoutOrientation(JList.VERTICAL); + this.colorsList = new JComboBox<>(this.getEyeObj().getColorsList()); + if (this.getEyeObj().getStrColor() != null) + this.colorsList.setSelectedItem(this.getEyeObj().getStrColor()); add(label); add(colorsList); }