• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Do something when object changes

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that creates the gui to my program. I need to update the gui when a object that is an instance field of the class changes. So I guess I need to make some sort of listener that listens for changes. I looked up Event listener but that doesnt seem to be what Im looking for. I made this thread here because this doesnt seem like a gui only problem, that is just the context of my problem.

Thanks

Edit:
Actually now that I think about it I need to update the gui when the other object tells my class. So I need to create a listener waiting to be told that it needs to update.
 
Sheriff
Posts: 24716
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually EventListener is exactly what you are looking for. Try PropertyChangeListener, or PropertyChangeSupport for something where the wiring code is already written.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a riff on the basic Observer/Observable design pattern. Is this a Swing program? If so, have you looked at implementing a ChangeListener?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really use PropertyChangeListener is good choice, whether or not your project is swing or j2se application, this listener is from package java.beans in standard jdk library so you don't invoke any else code
 
Max Diaz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I will check this PropertyChangeListener out.
 
Max Diaz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I kind of get the jist on how PropertyChangeListener and ChangeListener work. I understand that I create the class that handles the listening by implementing the ChangeListener interface. But thing object that I want to register the ChangeListener on is not of type JComponent. So this object does not have an addChangeListener() method. So I have to implement that too or am I thinking about this the wrong way?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Max Eddy wrote:Okay so I kind of get the jist on how PropertyChangeListener and ChangeListener work. I understand that I create the class that handles the listening by implementing the ChangeListener interface. But thing object that I want to register the ChangeListener on is not of type JComponent. So this object does not have an addChangeListener() method. So I have to implement that too or am I thinking about this the wrong way?



Could you tell us more about the problem? What type of object do you have that must be observed? How is it changed? Through user interaction? Other event?
 
Sheriff
Posts: 21821
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Max Eddy wrote:So this object does not have an addChangeListener() method. So I have to implement that too or am I thinking about this the wrong way?


You will need to add the addChangeListener and removeChangeListener methods. The easiest way is to forward these calls to your PropertyChangeSupport object.
 
Max Diaz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pete stein wrote:

Max Eddy wrote:Okay so I kind of get the jist on how PropertyChangeListener and ChangeListener work. I understand that I create the class that handles the listening by implementing the ChangeListener interface. But thing object that I want to register the ChangeListener on is not of type JComponent. So this object does not have an addChangeListener() method. So I have to implement that too or am I thinking about this the wrong way?



Could you tell us more about the problem? What type of object do you have that must be observed? How is it changed? Through user interaction? Other event?



The object has a list of another type of objects. I need to know when this list changes so I can update the gui with what is in the list.

[quot=Rob Prime]You will need to add the addChangeListener and removeChangeListener methods. The easiest way is to forward these calls to your PropertyChangeSupport object.

So I would need to add this class "PropertyChangeSupport" regardless if I decide to use ChangeListener or PropertyChangeListener? Or is there a simpler way to do it with just ChangeListener because I noticed that I need to mess with Java bean stuff If I go with PropertyChangeListener.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no pro at this, so if anyone has corrections, please post them, but something like this has worked well for me:
 
Rob Spoor
Sheriff
Posts: 21821
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would rename "notifyListeners" to "fireStateChanged" to keep in line with the usual naming of that method, and probably use lazy initialization of the ChangeEvent (even promoting it to a transient instance field since a ChangeEvent has no state except the source), but yeah, that looks good.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:I would rename "notifyListeners" to "fireStateChanged" to keep in line with the usual naming of that method, and probably use lazy initialization of the ChangeEvent (even promoting it to a transient instance field since a ChangeEvent has no state except the source), but yeah, that looks good.



Good point -- thanks! Yeah, it doesn't make sense to keep creating a new ChangeEvent object when it never "changes".
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No point in reinventing the wheel here folks. I use GlazedLists for this kind of stuff. It's really nice.
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!