Add listener to specific events like country selection.
[Persons_Comparator.git] / src / OriginView.java
CommitLineData
089fcbfc 1import javax.swing.*;
c8da8eb7
JB
2import java.awt.event.ItemEvent;
3import java.awt.event.ItemListener;
089fcbfc 4
c8da8eb7 5public class OriginView extends JPanel implements ItemListener {
6977e614 6 private Origin originObj;
883508ca 7 private JLabel label;
1a2ddb85 8 private JComboBox<String> comboBox;
1a2ddb85 9 private JLabel continentLabel;
6977e614 10
b974e749
JB
11 OriginView(Origin originObj) {
12 setOriginObj(originObj);
883508ca
JB
13 this.label = new JLabel();
14 this.label.setText("Origin");
1a2ddb85
JB
15 this.comboBox = new JComboBox<>(this.getOriginObj().getCountriesArray());
16 this.comboBox.setSelectedItem(this.getOriginObj().getCountry());
c8da8eb7 17 this.comboBox.addItemListener(this);
1a2ddb85
JB
18 this.continentLabel = new JLabel();
19 this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")");
883508ca 20 add(label);
1a2ddb85
JB
21 add(comboBox);
22 add(continentLabel);
089fcbfc
JB
23 }
24
6977e614
JB
25 public Origin getOriginObj() {
26 return originObj;
27 }
28
29 public void setOriginObj(Origin originObj) {
30 this.originObj = originObj;
31 }
c8da8eb7
JB
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 }
6977e614 41}