import java.awt.Color;
+import java.util.Arrays;
public class Eye implements Comparable<Eye> {
+ 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() {
this.color = Color.getColor(color);
} else {
- throw new IllegalArgumentException("Color must be" + new Eye().toString());
+ throw new IllegalArgumentException("Color must be" + new Eye().colorListToString());
}
}
return false;
}
+ private void sortColorList() {
+ Arrays.sort(this.colorsList);
+ }
+
/**
* @param eye
* @return
return distance.intValue();
}
- @Override
- public String toString() {
+ private String colorListToString() {
StringBuilder stringBuilder = new StringBuilder();
for (String c : colorsList) {
stringBuilder.append(" ");
public class EyeView extends JComponent {
private Eye eyeObj;
private JLabel label;
- private JList colorsList;
+ private JComboBox colorsList;
/**
* @param eyeObj
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);
}