Add listener to specific events like country selection.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 12 Jan 2019 13:15:43 +0000 (14:15 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 12 Jan 2019 13:15:43 +0000 (14:15 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Origin.java
src/OriginView.java
src/PersonView.java

index f14c2f27b2106e464d8e58b73a32e7811bc4b336..7d3e855c30ca7cb28690d408ffb61df29bde6ce9 100644 (file)
@@ -49,7 +49,7 @@ public class Origin implements Comparable<Origin> {
         return stringArrayList.toArray(rtArray);
     }
 
-    private String getContinentFromCountry(String country) {
+    public String getContinentFromCountry(String country) {
         int continentKey = -1;
         for (int i = 0; i < content2DArray.length; i++) {
             if (Arrays.asList(content2DArray[i]).indexOf(country) != -1) {
index a5df1ad987e4e353ad939763d51856f2c1f0bfc0..c347bf93c1d24f33c0bda8967435d16fefc43335 100644 (file)
@@ -1,10 +1,11 @@
 import javax.swing.*;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
 
-public class OriginView extends JPanel {
+public class OriginView extends JPanel implements ItemListener {
     private Origin originObj;
     private JLabel label;
     private JComboBox<String> comboBox;
-    //FIXME: this label should be refreshed on country selection.
     private JLabel continentLabel;
 
     OriginView(Origin originObj) {
@@ -13,6 +14,7 @@ public class OriginView extends JPanel {
         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);
@@ -27,4 +29,13 @@ public class OriginView extends JPanel {
     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() + ")");
+        }
+    }
 }
index 675f37f3ab7f72b17dabbaec15feee6a5f17a1bc..e703ef3c60b75d5020b87a0dc056ed9e32e0f400 100644 (file)
@@ -1,7 +1,9 @@
 import javax.swing.*;
 import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 
-public class PersonView extends JPanel {
+public class PersonView extends JPanel implements ActionListener {
     private int width = 400;
     private int height = 600;
     private Person personObj;
@@ -23,6 +25,7 @@ public class PersonView extends JPanel {
         setWeightView(new WeightView(this.personObj.getWeight()));
         setEyeView(new EyeView(this.personObj.getEye()));
 
+        this.compareButton.addActionListener(this);
         JLabel personLabel = new JLabel("Closest person found");
         personPanel.add(personLabel);
         setPreferredSize(new Dimension(this.width, this.height));
@@ -113,4 +116,9 @@ public class PersonView extends JPanel {
     public EyeView getEyeView() {
         return eyeView;
     }
+
+    @Override
+    public void actionPerformed(ActionEvent actionEvent) {
+
+    }
 }