From c8da8eb79910c7ea33dc2970ebd8d1d1db234a89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 12 Jan 2019 14:15:43 +0100 Subject: [PATCH] Add listener to specific events like country selection. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Origin.java | 2 +- src/OriginView.java | 15 +++++++++++++-- src/PersonView.java | 10 +++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Origin.java b/src/Origin.java index f14c2f2..7d3e855 100644 --- a/src/Origin.java +++ b/src/Origin.java @@ -49,7 +49,7 @@ public class Origin implements Comparable { 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) { diff --git a/src/OriginView.java b/src/OriginView.java index a5df1ad..c347bf9 100644 --- a/src/OriginView.java +++ b/src/OriginView.java @@ -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 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() + ")"); + } + } } diff --git a/src/PersonView.java b/src/PersonView.java index 675f37f..e703ef3 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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) { + + } } -- 2.34.1