• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Custom Event that nobody heard

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pardon me if I rage.

I've written events and eventlisteners with components and mouseclicks. No problem.

However I've got a weird situation which I am hoping to solve with a custom event. Trouble is one object in the project can't hear the event.

Project : start with class A1 and B1.

A Class A2 object is a member of A1, constucted in the A1's constructor.
A Class A3 object is a member of A2, constucted in the A2's constructor.
and
A Class B2 object is a member of B1, constucted in the B1's constructor.
A Class B3 object is a member of B2, constucted in the B2's constructor.

So A3 and B3 are not even cousins, in a manner of speaking, grand-cousins. However with a custom event I want to inform B3 of a secret number generated by A3.

Writing, registering and firing the event is easy. I've got a BlipEvent, a BlipListener interface and a MyBlipListener class, no problem.

The rules demand that the listenerList object be an EventListenerList object and a member of the class A3 where the event is fired.

This seems dumb.

Shouldn't this listenerList be in some place accessible by "any" class which has the need to detect this event? For example, in a class full of static public things? How else is B3 supposed to hear it?

How can my B3 object, buried in the hierarchy of the project, gain access to the listenerList when it's part of a distant cousin? And if B3 had access to A3 (wounding encapsulation by passing "this" pointers to everyone) then why am I struggling with a custom event? If B3 had access to A3 then I'd just walk in and take the info.

Or must I go to the common parent of both A3 and B3 as a "middle-man"?

What am I thinking wrong?>
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Venting helps.

I've just found that one needs an intermediary, a common ancestor. I created class Grandpa which holds both A1 and B1 and all their children and links to the children's children.

In B3 I created the listener and in A3 I created the listenerList. Then in Grandpa, in the ctor, I did :

And in A3 I didn't create any BlipListener at all !!!

What a relief !
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Elwood wrote:I've just found that one needs an intermediary, a common ancestor. I created class Grandpa which holds both A1 and B1 and all their children and links to the children's children.
In B3 I created the listener and in A3 I created the listenerList. Then in Grandpa, in the ctor, I did :
And in A3 I didn't create any BlipListener at all !!!


I suspect you would have made things a whole lot easier for yourself (and certainly us) if you'd used meaningful names for your classes. Your description so far reminds me of one of those nightmare questions you get in the SCJP exam.

Names are incredibly important because they provide context, and good ones might have helped you to your solution a bit quicker.

Winston
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meaningful names for your classes



such as?

Honestly, I don't know what you mean.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
once again i find myself agreeing with Winston. i usually don't even bother reading "lets pretend" code.
meaningful names. for example ImageViewer or AnalogClock. a name that means something(tells what the program does).
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Elwood wrote:such as?


I honestly don't know, because they're your classes and I couldn't really make head or tail of your question - but definitely NOT 'B3'.

Winston
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the OP's doing strange things with different sizes of paper.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Perhaps the OP's doing strange things with different sizes of paper.


Ah. A clear mind on the subject. Does tend to suggest a PaperSize superclass we weren't told about...and wouldn't that other class then be a ClipListener?

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:and wouldn't that other class then be a ClipListener?


 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I honestly don't know, because they're your classes and I couldn't really make head or tail of your question



RTF text

A Class A2 object is a member of A1, constucted in the A1's constructor.
A Class A3 object is a member of A2, constucted in the A2's constructor.
and
A Class B2 object is a member of B1, constucted in the B1's constructor.
A Class B3 object is a member of B2, constucted in the B2's constructor.




a name that means something(tells what the program does).



: it demonstrates a concept

Still no ideas of how the classes 'must' be named?

How about Grandpa <-- Son1 <-- Grandson1 <-- Greatgrandson1 etc. ?? Acceptable or no?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Elwood wrote:It demonstrates a concept


Even more important then that you use meaningful names.

How about Grandpa <-- Son1 <-- Grandson1 <-- Greatgrandson1 etc. ?? Acceptable or no?


Not really. It doesn't tell us any more than we knew already.
From what you said originally, I gather this problem came up in a real-life situation, so why not use the class names that produced it?

However, I've since re-read you original post, and I think I now follow what you're asking.

How can my B3 object, buried in the hierarchy of the project, gain access to the listenerList when it's part of a distant cousin? And if B3 had access to A3 (wounding encapsulation by passing "this" pointers to everyone) then why am I struggling with a custom event? If B3 had access to A3 then I'd just walk in and take the info.


I think "wounding encapsulation" is a red herring here.

A3 and B3 are only related if they share a common ancestor, which doesn't appear to be the case; so they are basically unrelated classes (not even "distant cousins"). And even if they do share some distant antecedant (java.awt.Component?) it's unlikely that it relates them in any meaningful way, except to say that they're part of the same GUI.
The only reasonable way therefore to put the two classes in direct communication is to make one the Listener and the other the "listened to" (or Observer and Observed). Trying to design an event which is only "recognizable" by certain classes or points in a hierarchy sounds like a recipe for code-bloat to me, and is only likely to slow down communication.

My advice: Keep it simple.
But I'm happy to be disproved; I'm certainly no great GUI expert.

Winston
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Winston!!

Now I've got some info!

My project calculates the orbits of satellites around gravity sources plotting ellipses, parabolas, hyperbolas and even straight lines when no gravity source is present.

I've got a FrameView object at the top. In it is a SpacePanel object with all the visual goings-on. Another member of the FrameView is the Scenario class object which I am trying model as top-dog, know-it-all, Big-Brother class. The Scenario holds an array of SpaceObjects (Satellites and GravitySources) in my universe. Each Satellite holds its own Trajectory object.

And the one Scenario object holds a Hopper class which ticks off every second or so and signals all the Satellite objects to move one step further along their pre-calcuated trajectories.

Hopper (member of Scenario) generates an ActionTimer event and I want the Trajectory objects (grandchildren of Scenario) of all the Satellites to move to the next step.

So one member/child of Scenario had to communicate with other grandchildren/grand-members of Scenario.

See how complicated it gets? That's why I dumbed it down to A1, A2 etc.

BTW - I would have passed 'this' arguments to member objects to help communication but my IDE, Netbeans 7, gives a mild warning that doing so violates encapsulation of the parent. If I follow their advice then the only solution is to fire an event to it's parent to transfer info.

It's a red herring--you mean not a real danger or is it an ugly but necessary evil?

I hope you understand that this whole project is a just-for-fun thing I'm doing. Still though, I'd like to do it right.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Elwood wrote:I've got a FrameView object at the top. In it is a SpacePanel object with all the visual goings-on. Another member of the FrameView is the Scenario class object which I am trying model as top-dog, know-it-all, Big-Brother class. The Scenario holds an array of SpaceObjects (Satellites and GravitySources) in my universe. Each Satellite holds its own Trajectory object.

And the one Scenario object holds a Hopper class which ticks off every second or so and signals all the Satellite objects to move one step further along their pre-calcuated trajectories.

Hopper (member of Scenario) generates an ActionTimer event and I want the Trajectory objects (grandchildren of Scenario) of all the Satellites to move to the next step.


Much better. Now we have some context to work with.

One thing I can see is that you're mixing up the problem (calculating and traversing the trajectories of bodies) with the representation (Frames and Panels and all that other Swing crap). DON'T.

In theory, you should be able to plug your Scenario class into any sort of "view" object, be it a GUI, a printer, or even a character-based terminal and still have it work, because as far as the problem is concerned, the interactions aren't between Swing components but between your SpaceObjects. The only thing the GUI should be concerned with is painting them (and possibly dealing with simple user commands like Start, Stop and Pause).

Now your problem becomes one of representing the position and behaviour of your SpaceObjects in 2D space (or Euclidian - unless you fancy tackling curved space geometry ), NOT of how to draw them; and I suspect you'll find that your hierarchy is actually quite simple:
A SpaceObject (which could be an interface), with two subtypes hanging off it (Satellite and GravitySource).
I suspect also that your Scenario and Hopper classes will become wrappers to collections of SpaceObjects, not their parent.

However, if I've misunderstood something, do feel free to correct me.

Winston
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic