Add listener to specific events like country selection.
[Persons_Comparator.git] / src / OriginView.java
1 import javax.swing.*;
2 import java.awt.event.ItemEvent;
3 import java.awt.event.ItemListener;
4
5 public class OriginView extends JPanel implements ItemListener {
6 private Origin originObj;
7 private JLabel label;
8 private JComboBox<String> comboBox;
9 private JLabel continentLabel;
10
11 OriginView(Origin originObj) {
12 setOriginObj(originObj);
13 this.label = new JLabel();
14 this.label.setText("Origin");
15 this.comboBox = new JComboBox<>(this.getOriginObj().getCountriesArray());
16 this.comboBox.setSelectedItem(this.getOriginObj().getCountry());
17 this.comboBox.addItemListener(this);
18 this.continentLabel = new JLabel();
19 this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")");
20 add(label);
21 add(comboBox);
22 add(continentLabel);
23 }
24
25 public Origin getOriginObj() {
26 return originObj;
27 }
28
29 public void setOriginObj(Origin originObj) {
30 this.originObj = originObj;
31 }
32
33 @Override
34 public void itemStateChanged(ItemEvent event) {
35 if (event.getStateChange() == ItemEvent.SELECTED) {
36 Object item = event.getItem();
37 getOriginObj().setContinent(getOriginObj().getContinentFromCountry(item.toString()));
38 this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")");
39 }
40 }
41 }