From d75d0a00d45e0f62b494a7282a5902e63df32b1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 11 Jan 2019 15:54:20 +0100 Subject: [PATCH] Change the EyeView for a JComboList, store the color string inside the Eye class and choose the color string in the view. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Eye.java | 28 ++++++++++++++++++++++++---- src/EyeView.java | 8 ++++---- 2 files changed, 28 insertions(+), 8 deletions(-) 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); } -- 2.34.1