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

How can two different objects receive notification of the same event?  RSS feed

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening Colleagues.

I'm not sure of if this is in the correct forum - apologies if it is not.

The scenario:

* A pop-up menu generates an array of rectangle objects; each rectangle having random x.y coords and colour.
* The number of rectangles in the array is user selected from the popup list.
* The APopUp class has a 'getter' method that returns the array.
* The pop-up object is instantiated in an object of the AFrame class.
* The AFrame object needs to get the array each time the popup changes its state.

The problem:
.
* As I see it, the AFrame object needs to informed of the pop-up's state of change event or does the problem lie in how I have decided
which responsibilities each class is to have? I've tried to be as logical as possible during planning.

In the days of FORTRAN or BASIC we would hammer a solution through with brute force, but I would like to keep to Java's philosophy
of encapsulation and clean interfaces and that it seems to has generated a impasse of brain warping proportions.

As always, any help has been, and will be, helpful.
















 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your frame have an array at all? You should have classes which encapsulate the model, which means the data, and others for the display. Don't confuse the two.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Campbell,

OK this is why I thought the frame should receive the array of rectangles:

* because the long term aim is to display these boxes in the frame and maybe later I could make them move in the frame as well.
* I would need to use the frames paint method and therefore the frame would need the array of rectangle objects.

(by the way, these scenarios might seem a bit silly: I've dreamt them up just for developing my skills/understanding - I hope they don't annoy).


You should have classes which encapsulate the model, which means the data, and others for the display.


I read something about it but didn't fully understand - I'll have to go back to it.

 
Omar Rashwan
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what ever the logic you used in your program,
but the Question is how can two different objects receive notification of the same event ?
the answer is : by registering recieving objects as listeners to the event generating object,
for example


But in your code if you are not going to change it simply
make this
in you ApopUp class create an ItemListener instance //which will be the AFrame Object


in ApopUp constructor add an Arg like this

in ApopUp class add this code to the last line of itemStateChanged(ItemEvent e) this code

in the Aframe class change this code
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey Hey !.. thank you Omar.

After analysing your example and applying your suggested changes, my code worked!

I do need to look at how I have modelled the problem though.

I think that the correct modelling of the problem will lead to easier coding.

I've been searching google "modelling problems and java programming" and the term "Unified Modelling Language" keeps coming up - it seems to be tool that helps to model the problem into classes and the interaction between them.

I think I will devote some of my time to UML in parallel with developing my programming skills - what do you think guys?



 
Omar Rashwan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes its very good thing to learn UML as a programmer if you have time keep it up ( ^ _ ^ ).
And you are absolutely correct about planning and analyzing the solution before coding it. specially if its
Complicated and big. and really saves a lot of our times.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!