Results 1 to 13 of 13
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Listen for changes in a custom Object?

    Hi, I have a class called Order, and this is a very simple example:

    Java Code:
    public class Order {
        List<Product> orderList;
        Order() {
            orderList = new ArrayList<Product>();
        }
        public void addProduct(Product p) { orderList.add(p); }
    }

    I create an instance of the class like this:

    Java Code:
    Order currentOrder = new Order();

    Then I have a GUI to edit the order, add products to it, etc.
    I also have a JTable to display the currentOrder.

    The JTable is updated like this:

    Java Code:
    public void updateOrderTable() {
        orderTable.setModel(new OrderTable(currentOrder).getTableModel());
    }

    Note that OrderTable is also a custom class to create a JTable for a given Order, and getTableModel() returns the table model.

    Now, there are many different parts of the GUI that can change an order,
    and every change needs to be reflected in the OrderTable.

    But I want to avoid using updateOrderTable() after each button or control.

    So, how do I listen to a change in the instance variable 'currentOrder' so
    that every time it changes a call is made to updateOrderTable()... ?

    Any advice or help?
    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,449
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    So, how do I listen to a change in the instance variable 'currentOrder' so
    that every time it changes a call is made to updateOrderTable()... ?

    Any advice or help?
    Thanks
    Add a TableModelListener to your TableModel; it is called each time your model changes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Thanks JosAh... actually the table model is only updated by the updateOrderTable() function, but what I want is to listen for changes in an Order object so that as the order is edited it is automatically displayed.

    I was wondering if I can make the class Order implement some sort of Listener interface and then use the currentOrder.addSomeListener() method... ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Yes, you can create/write your own listener. There would be an interface, an event and the Order class would need the addListener and removeListener methods and a queue to hold the list of listeners. Any change the Order class would want to announce would use the queue of listeners to call the interface method and pass it the event.

    There are probably Listeners that exist that would do some of it for you.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Hi Norm, thanks for that info. So I've established now that there is a way... I guess my question should've been are there any Listeners to do that for me?

    I tried to google this before but there wasn't any relevent results.

    I think the Listener Interface I want is one of these, but I'm not sure if this is the right one...

    java.beans.PropertyChangeListener
    Or
    javax.swing.event.ChangeListener

    Which one should I implement and could you direct me to a good place to learn about creating the addListener, removeListener methods?

    Thanks again for your help

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    The add and remove methods are very simple. They work with the method that calls the listeners. You'll need a queue to hold the listeners. The add method adds to it and the remove method removes an entry. The method that notifies the listeners goes thru the queue, pulls out the listeners one by one and calls the method defined in the interface, passing it the event object you've created.

    If you have access to the source for Java, you could look up how the Java people have done it by looking at a simple class that has listeners.

    The ChangeListener sounds like a good fit.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,449
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    Hi Norm, thanks for that info. So I've established now that there is a way... I guess my question should've been are there any Listeners to do that for me?

    I tried to google this before but there wasn't any relevent results.

    I think the Listener Interface I want is one of these, but I'm not sure if this is the right one...

    java.beans.PropertyChangeListener
    Or
    javax.swing.event.ChangeListener

    Which one should I implement and could you direct me to a good place to learn about creating the addListener, removeListener methods?

    Thanks again for your help
    Use the PropertyChangeSupport class; simply delegate the adding/removing of listeners to that object and signal it (in your class) when something has changed; read the API documentation for details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Okay, so I've decided to go for the PropertyChangeListener so I can use a PropertyChangeSupport field to handle the queue as JosAh suggested.

    Thanks guys for your help.
    :)

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Sorry, I have a quick question on implementing this.

    I've set up the class like this now:

    Java Code:
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    public class Order { //doesn't implement PCListener
        //fields & methods for Order as usual
        ...
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
     
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.addPropertyChangeListener(listener);
        }
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.removePropertyChangeListener(listener);
        }
    }

    Do I need to use 'propertySupport.firePropertyChange(...)' in each setter method I want to listen for changes in?
    (and is it true that if I don't the property change will not be listened to?)

    That's all, thanks.
    Last edited by ozzyman; 05-16-2011 at 10:50 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    You should write a small test program and try the classes out and see what happens.
    I think you call the fire... method when the change is made to the object that has the listeners and you want to notify the listeners.

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Thanks norm, well actually I tried out my program in netbeans and nothing appeared to happen (i.e. the JTable wasn't updated), so I thought it would be because of not firing the events.

    Just going to try adding fire... methods in certain methods, but I was just worried of firing events in instances of the class that I havent added any Listeners to...

    Yes, so adding fire events makes it work (Sorry I didn't try that it first!)

    Now i'm just wondering if anyone can tell me about the firing events in objects that don't have the listeners?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    firing events in objects that don't have the listeners
    Write the test program and see what happens when there are not listeners.

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh yeah, I did remove the listener and change that property but nothing seems to happen, but was wondering if it was bad practice to do so.

    Anyway, it all seems fine.

    Thanks so much for your help!

Similar Threads

  1. Replies: 5
    Last Post: 12-15-2010, 02:22 AM
  2. Cast object to custom class problem
    By trader5050 in forum New To Java
    Replies: 7
    Last Post: 11-15-2010, 10:47 AM
  3. Listen for changes in a JTextField
    By matzahboy in forum AWT / Swing
    Replies: 5
    Last Post: 10-13-2009, 04:15 PM
  4. Cannot Listen TAB key
    By sh4dyPT in forum AWT / Swing
    Replies: 5
    Last Post: 07-16-2009, 01:45 PM
  5. Consuming web service with return custom object
    By dream_ in forum Networking
    Replies: 1
    Last Post: 04-16-2009, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •