Results 1 to 11 of 11
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default communication between classes

    Imagine there are two classes (actually there five, but lets simplify the example). Class A, the top class, contains the final static-void-main method, and class B sets all GUI stuff (please do not ask me way). A button event of class A should trigger large sequences of things and the user (besides doing other things at the GUI panel of class B) could push the button at any time.
    How should this be implemented? A wait loop at class A, will block other GUI activity of class B! The events of the object of class B, created at class A, are not noted right away to class A (events happen in class B).

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post no wait states

    Java Code:
    class A {
      B b;
      A(B bee){
      b = bee;
    Class A now can do and call methods of a class B on the name "b" thus:
    Java Code:
     b.doSomething(data);
    In the class that has the button pushing stuff, we have a remarkably simple adjunct but that requires you grasp this first,.... then we still have to drive the controller class in a non-blocked manner using the clock I described in the don't ask me to which you refer.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    A button event of class A
    This is a contradiction if all the GUI is in class B.
    trigger large sequences of things
    These things should not be done on the thread of the listener (also call EDT). When the button is pressed, start a thread to do the "large sequences". This will leave the GUI alive to allow the user to
    push the button at any time
    The events of the object of class B, created at class A, are not noted right away to class A (events happen in class B).
    No idea what this means.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    so b is a variable of the of type B. A´s constructor takes bee is an argument passed in as type B assigned to b... you might want to post this in eranga´s quiz?

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    norm you are right it is a button event of class B

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    "The events of the object of class B, created at class A, are not noted right away to class A (events happen in class B)."

    The events happen in class B
    Class A creates an object of class B. lets say b
    (B b = new B();
    b.button reads out the button at class A
    BUT I DO NOT KNOW WHEN THE BUTTONS GETS PUSHED
    So I start a thread with a spinning loop
    do {} while (button pressed)
    exit the run()
    but that is what you do not like, norm?
    (anyway my cpu has nothing better to do)

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default progress

    this is moving in the right direction,... we have several classes to tangle with and as well a method call can do the passing in of a reference - that is more to your thinking than some rigid style. You will need several classes...

    a GUI class
    a Clock class
    a MasterController
    one or more data classes

    Do not drive the master controller with events, which is what you will try to do.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Thanks nick, its late in spain
    I´ll go for a sleep

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    So I start a thread with a spinning loop
    do {} while (button pressed)
    exit the run()
    Then what??? when you exit the loop?
    Can you have the button press start whatever is supposed to be started when the loop exits?

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post Norms got it

    As usual, Norms on top of the systems issues at hand.

    willemjav, it sorta runs like this.....
    Java Code:
    do{
    if(button_pressed){shipData();yield();}
    else{yield, sleep or whatever}
    that's in the PlaySequence class, which has a boolean... run / stop

    then in MainControllerClass:
    Java Code:
    ControllerClass{
       PlaySequence sequencer;
       ControllerClass(PlaySequence ps){
       sequencer = ps;
    }
    and in the ControllerClass we have in the event handler:
    Java Code:
    if(event typeof button event){
       sequencer.button_pressed = true / false;//
    which can also be done by a method call.

    The work at hand is not unlike looking at one's image in a mirror in the morning when waking up and at that instant where you gain enough conciousness to differentiate the image in the mirror from the actual coporeal body and as well disentangle that from your thoughts, which is also contradistincted from the thoughts of others......

    Some languges, many non ISO Latin-1 languages, do not even have words for this, probably some relaxed contemplation is best for this work. That is what works for me. In fact I no longer even put this stuff in a main.....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Trying to help you with your question brings us to class design and object-oriented programming ideas. Although there are many ways to put things together there are some general ideas/guidelines that seem to work well. Ususally we put the main method in the gui class and center the user-interaction/event-listening activity in it. The event listeners then call methods in the various classes to make things happen.
    This gives the user direct control of what happens in the app and helps avoid awkward class communication situations. Sometimes it is wise to step back and have a look at the overall design and reconsider how you've put things together.
    It is possible that you have worked out a model for your music and are later/now trying to find a way to show it in a gui. If so then this may be a good time to consider design/redesign.
    However, if you are well past this point and your class design makes sense to you and you want to continue to develop this idea of having class A "wait" for some user input from class B I have two suggestions:
    1 — have class A implement the ActionListener interface and register it as a listener with the button in (your gui) class B. Class A will then know when the user presses the button and is ready to trigger the large sequence.
    2 — if there is a process running in A and it must block/wait for the user to press the button in B (sounds like what you are asking for) then you could try something along the lines of the producer-consumer technique (illustrated in the concurrency trail in the java tutorial) which uses the wait and notify/notifyAll methods with a while loop.

Similar Threads

  1. Jar and War communication in an Ear
    By madanmohanp in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2008, 01:39 PM
  2. inter process communication
    By ibtehal in forum Networking
    Replies: 5
    Last Post: 06-23-2008, 01:35 AM
  3. Communication with c++
    By mathias in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:47 AM
  4. how To Use Https Connection For Communication
    By fred in forum Advanced Java
    Replies: 2
    Last Post: 08-01-2007, 04:59 PM
  5. applet servlet communication
    By hardc0d3r in forum Java Applets
    Replies: 1
    Last Post: 07-12-2007, 06:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •