Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - Listener or Observer?

Options
  • 13-04-2009 5:34pm
    #1
    Registered Users Posts: 647 ✭✭✭


    Hi,
    I have class (call it A) which needs to know when an ArrayList in another class (B) is updated. From what I can gather, my two options are to extend the ArrayList class to allow me to add my own changeListener to it, or to extend observable with the class which contains the ArrayList in question. Which would be a more appropriate solution? B isn't a particularily big class if it makes a difference.


Comments

  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Rather than worrying about the pattern, what are you actually trying to describe?
    Listeners are generally good, but it's important to choose the right tools for the situation.


  • Registered Users Posts: 647 ✭✭✭simonw


    Rather than worrying about the pattern, what are you actually trying to describe?
    Listeners are generally good, but it's important to choose the right tools for the situation.

    In the GUI i've a dropdown box which will show the contents of the ArrayList. New objects can be added to the ArrayList elsewhere in the GUI, so I need to keep the dropdown box up to date.


  • Registered Users Posts: 885 ✭✭✭clearz


    Listener and Observer are both parts of the same Observer pattern which is exactly what you want here as far as I can see with the limited information supplied. Your problem is one of the main uses of the observer pattern. Subclassing GUI classes is not the way to go.


  • Registered Users Posts: 647 ✭✭✭simonw


    clearz wrote: »
    Listener and Observer are both parts of the same Observer pattern which is exactly what you want here as far as I can see with the limited information supplied. Your problem is one of the main uses of the observer pattern. Subclassing GUI classes is not the way to go.

    What other information do you want?


  • Registered Users Posts: 981 ✭✭✭fasty


    Does Java do data binding for controls?


  • Advertisement
  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    So it's easy, like most GUI components, just listen to the dropdown box, not the list. When it registers an updated action, you should hear it in your listener implementation and do the work.

    http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#listeners


  • Registered Users Posts: 885 ✭✭✭clearz


    simonw wrote: »
    What other information do you want?


    Well the information that you supplied in your second post which I had not seen telling us that you are using a combobox is pretty relevant. I would say you don't need the arraylist at all and just add/remove items directly to the combo box. And as Civilian_Target says you can add a listener directly to the combobox which should fire when items are added/removed from it. Also I'm sure there is a method that will return the entire contents of the combo in an arraylist if needed.


Advertisement