Results 1 to 11 of 11
Thread: communication between classes
- 08-31-2008, 10:50 PM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 14
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).
- 08-31-2008, 11:09 PM #2
no wait states
Java Code:class A { B b; A(B bee){ b = bee;
Java Code:b.doSomething(data);
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-01-2008, 01:26 AM #3A button event of class A
trigger large sequences of thingspush the button at any timeThe events of the object of class B, created at class A, are not noted right away to class A (events happen in class B).
- 09-01-2008, 01:26 AM #4
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 14
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?
- 09-01-2008, 01:28 AM #5
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 14
norm you are right it is a button event of class B
- 09-01-2008, 01:36 AM #6
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 14
"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)
- 09-01-2008, 01:37 AM #7
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
- 09-01-2008, 01:50 AM #8
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 14
Thanks nick, its late in spain
I´ll go for a sleep
- 09-01-2008, 04:19 AM #9So I start a thread with a spinning loop
do {} while (button pressed)
exit the run()
Can you have the button press start whatever is supposed to be started when the loop exits?
- 09-01-2008, 05:46 PM #10
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}
then in MainControllerClass:
Java Code:ControllerClass{ PlaySequence sequencer; ControllerClass(PlaySequence ps){ sequencer = ps; }
Java Code:if(event typeof button event){ sequencer.button_pressed = true / false;//
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
- 09-01-2008, 11:08 PM #11
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
-
Jar and War communication in an Ear
By madanmohanp in forum Advanced JavaReplies: 1Last Post: 08-02-2008, 02:39 PM -
inter process communication
By ibtehal in forum NetworkingReplies: 5Last Post: 06-23-2008, 02:35 AM -
Communication with c++
By mathias in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:47 AM -
how To Use Https Connection For Communication
By fred in forum Advanced JavaReplies: 2Last Post: 08-01-2007, 05:59 PM -
applet servlet communication
By hardc0d3r in forum Java AppletsReplies: 1Last Post: 07-12-2007, 07:58 PM
Bookmarks