0

Hello I have ItemChangeListener... but I want this to be responsible for more than one view. ( If this is not the right way to do this please tell me how else ) So that when I choose an item in a JComboBox I get to work with that object I selected

public class ItemChangeListener implements ItemListener {

Persoon selectedPerson;
PersoonView view;


    public void itemStateChanged(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.SELECTED) {
            Object item = event.getItem();
            // do something with object
            if(item instanceof Persoon) {
                this.selectedPerson = (Persoon) item;
                view.setOverzicht(this.selectedPerson);
            } else {
                this.selectedPerson = null;
            }
        }
    }

}

But as u can see right now.. It only works with one specific view the PersoonView view class. How should I modify this to work with more than one view?

edit: In my view I do this ( right now I have 2 view )

personenList = new JComboBox();
    for (Persoon p : app.getPersonen()) {
        personenList.addItem(p.getNaam());
    }

    personenList.addItemListener(new ItemChangeListener());

Everytime I select another item I want to get the selectedPerson to work with in that view for example I want to do this in my ActionListener that is in my view.

selectedPerson.voegtoeRek(new Rekening(Integer.parseInt(nieuwnr.getText()), selectedPerson));

So practically I need to find some way to get that variabel out of ItemChangeListener.

Reshad
  • 2,570
  • 8
  • 45
  • 86
  • there I can't see any issue – mKorbel Oct 22 '13 at 11:42
  • I have 2 view that call this personenList.addItemListener(new ItemChangeListener()); Both this views have a method to update a JTextArea with info about the item selected. How can the itemchangelistener let the correct methode know what view to choose? – Reshad Oct 22 '13 at 11:53
  • do you [mean (only sceleton)](http://stackoverflow.com/a/19511300/714968), or there could be 2D array as source for JComboBox, one, two three JComboBoxes (somehting like as `Vector`, Map or AbstractTableModel too), then selection in JComboBox can returns any value from linked, related row – mKorbel Oct 22 '13 at 12:13
  • btw this question is about 1. closing votes (unclear what ...) 2. edit with an SSCCE, and better description – mKorbel Oct 22 '13 at 12:14
  • I modified my question I hope its clear now – Reshad Oct 22 '13 at 12:23

1 Answers1

0

create an interface with setOverzicht(Persoon person) method which can be inserted in listener constructor. The only thing you need then is to implement the interface if you want to use this listener.

AppX
  • 528
  • 2
  • 12
  • I mean more like.. Do I have to let my itemChangeListener return the person item? ( therefore make a getter ) so that I can work with it in my view or what? and if I have to make a getter how should this look like? – Reshad Oct 22 '13 at 11:43