• 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 ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Creating User-Defined Events  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all know how the events are generated and how to response to those events. However all these events are in-build.
For example ActionEvent is generated, when u click a button.
Similarly, I want my own event should be generated when certain condition is satisfied...For example, LessBalanceEvent should be generated when a condition such as balance < 500 rupees is satisfied and I also having a doubt that how can I register my LessBalanceEvent to a object by using addBalanceListener()
I would be thankful one and all, who can assit me in this. I would also thank you, if you gave sample program.
Thank you in advance
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

There's nothing magical about generating or firing an event. If you had an Account object it might have something like this:

LowBalanceListener would be an interface you created to define the lowBalanceEvent() method. You'd also need add and remove listener methods and that kind of housekeeping stuff.

You can get a lot of this functionality from Observable in the standard Java API. Before I found Observable I wrote my own little event publisher which is much simpler and more flexible but requries more hand-written implementation. Let me know if that sounds interesting. But look into Observable and Observer first.

Let us know if that helps!
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!