Add listener to specific events like country selection.
[Persons_Comparator.git] / src / OriginView.java
index 2fc4189df734149123d81aba4f2ed3a0c491190e..c347bf93c1d24f33c0bda8967435d16fefc43335 100644 (file)
@@ -1,13 +1,25 @@
 import javax.swing.*;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
 
-public class OriginView extends JComponent {
+public class OriginView extends JPanel implements ItemListener {
     private Origin originObj;
-
-    OriginView() {
-    }
+    private JLabel label;
+    private JComboBox<String> comboBox;
+    private JLabel continentLabel;
 
     OriginView(Origin originObj) {
         setOriginObj(originObj);
+        this.label = new JLabel();
+        this.label.setText("Origin");
+        this.comboBox = new JComboBox<>(this.getOriginObj().getCountriesArray());
+        this.comboBox.setSelectedItem(this.getOriginObj().getCountry());
+        this.comboBox.addItemListener(this);
+        this.continentLabel = new JLabel();
+        this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")");
+        add(label);
+        add(comboBox);
+        add(continentLabel);
     }
 
     public Origin getOriginObj() {
@@ -17,4 +29,13 @@ public class OriginView extends JComponent {
     public void setOriginObj(Origin originObj) {
         this.originObj = originObj;
     }
+
+    @Override
+    public void itemStateChanged(ItemEvent event) {
+        if (event.getStateChange() == ItemEvent.SELECTED) {
+            Object item = event.getItem();
+            getOriginObj().setContinent(getOriginObj().getContinentFromCountry(item.toString()));
+            this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")");
+        }
+    }
 }