Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

custom event for components  RSS feed

 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

thanks for any help / advice.

i was wondering on the best way to listen for custom events from components.

Say if i have a class (ClassA) that wraps (holds) a component (ClassB).... and i want that class (ClassA), to listen for custom events from the inner component class (ClassB).

For example, if i have a JTabbedPane, and it listens for when the tab is changed, i can pick that up from within this component, but what if i want this listened for in the outer class?

Whats the best way of doing this?

Those familiar with PureMVC (its a Mediator that is looking after a component).

Regards, snapple
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you rephrase your requestion? Who is listening to who?

Maybe an example like,
class a contains class b, and
class b contains class c, and
class c listens to class b's change-tab-events?
and what do you need to do now?
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesus Angeles,

Thanks for your reply.

I'm afraid dont really know how to rephrase it any clearer than:

i have a class (ClassA) that wraps (holds) a component (ClassB).... and i want that class (ClassA), to listen for custom events from the inner component class (ClassB).


Im not sure whats unclear about that sentence?

Regards, snapple
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is a class C inside class B, that generates an event, that class A wants to listen to?

If class C must remain 'invisible' to class A, then you must involve class B. That is, Class B can refire the event, or class B can expose a get method letting class A see class C and thereby register and listen to it, etc.

If class C is visible to class A, then class A can simply register and listen to it.
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesus Angeles,

wooooa - where did ClassC come from?



ClassA "has a" (contains) ClassB. Thats it.

ClassB could be e.g. a TabbedPane, and ClassA is just an ordinary class.

I don't want to pass any references in to any classes... i want to do it via the event listener model.

Regards, Sam
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam wootton wrote:I don't want to pass any references in to any classes...

Well, classB needs a reference to something to which it can send events. Doesn't have to be a reference of classA, though - it could be some event listening interface, like java.util.Observer. The drawback with that particular approach is that classB would have to extend java.util.Observable, which may or may not be feasible. But Observable doesn't do much, so you could implement something along these lines yourself with little effort.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!