Calling an instantiated object's method from another object at the same level?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Calling an instantiated object's method from another object at the same level?

  1. #1
    Lim Wing Hoe Guest

    Calling an instantiated object's method from another object at the same level?

    I have a log window JInternalFrame that is instantiated when I load the MDI.

    class : EventsLog
    method : writeToEventsLog(String event)

    In the MDI, this EventsLog is at the same level with other objects that are
    instantiated, e.g. PublicChat window, PublicWhiteBoard window. All of them
    are added to the MDI desktop using desktop.add method that is available for
    JDesktopPane.

    level 0: MDI
    level 1: EventsLog, PublicChat, PublicWhiteBoard

    I want to have both PublicChat and PublicWhiteBoard to be able to call the
    method in the EventsLog and update the textarea in the EventsLog.

    If I do the below:

    class PublicChat
    { private EventsLog eventsLog = new EventsLog();
    ....
    //somewhere down here I called the method
    eventsLog.writeToEventsLog("this is an event");
    }

    It doesn't update the textarea in the eventsLog.

    I'm assuming that the EventsLog object that I instantiated at the class
    PublicChat is at the level below PublicChat and it doesn't reflect to the
    EventsLog object at level 1.

    level 1: PublicChat
    level 2: EventsLog

    I don't want to have another object instantiated and running another copy of
    the process. I just want the EventsLog object at level 1 to be accessible by
    all other objects at the same level.

    What can I do to rectify the error?
    Hoping for your quick reply.
    Thanks.
    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    ICQ: 2213281
    Email: winghoe@hotmail.com
    www: http://pwp.maxis.net.my/winghoe
    ---------------------------------------------------------------







  2. #2
    Paul Clapham Guest

    Re: Calling an instantiated object's method from another object at the same level?

    I don't know what this "level" stuff is, but your PublicChat class appears
    to contain a private object of class EventsLog. Is this the same EventsLog
    object that you have added to your MDI? Or did you create ANOTHER EventsLog
    object somewhere else and add it to the MDI?

    If the PublicChat object is the one logging the events, then it needs to
    export its EventsLog object so that it can be added to the MDI. Sample
    code, if you don't already have something like this:

    /* in class PublicChat */
    public EventsLog getEventsLog() {
    return eventsLog;
    }

    /* in main program */
    desktop.add(yourPublicChatObject.getEventsLog());

    There are other solutions to this problem, such as providing a method to
    pass an EventsLog object into the PublicChat object.

    PC2

    Lim Wing Hoe <winghoe@hotmail.com> wrote in message
    news:39f17376@news.devx.com...
    > I have a log window JInternalFrame that is instantiated when I load the

    MDI.
    >
    > class : EventsLog
    > method : writeToEventsLog(String event)
    >
    > In the MDI, this EventsLog is at the same level with other objects that

    are
    > instantiated, e.g. PublicChat window, PublicWhiteBoard window. All of them
    > are added to the MDI desktop using desktop.add method that is available

    for
    > JDesktopPane.
    >
    > level 0: MDI
    > level 1: EventsLog, PublicChat, PublicWhiteBoard
    >
    > I want to have both PublicChat and PublicWhiteBoard to be able to call the
    > method in the EventsLog and update the textarea in the EventsLog.
    >
    > If I do the below:
    >
    > class PublicChat
    > { private EventsLog eventsLog = new EventsLog();
    > ...
    > file://somewhere down here I called the method
    > eventsLog.writeToEventsLog("this is an event");
    > }
    >
    > It doesn't update the textarea in the eventsLog.
    >
    > I'm assuming that the EventsLog object that I instantiated at the class
    > PublicChat is at the level below PublicChat and it doesn't reflect to the
    > EventsLog object at level 1.
    >
    > level 1: PublicChat
    > level 2: EventsLog
    >
    > I don't want to have another object instantiated and running another copy

    of
    > the process. I just want the EventsLog object at level 1 to be accessible

    by
    > all other objects at the same level.
    >
    > What can I do to rectify the error?
    > Hoping for your quick reply.
    > Thanks.
    > --
    >
    >
    >
    > Best Regards,
    > Wing Hoe
    > ---------------------------------------------------------------
    > ICQ: 2213281
    > Email: winghoe@hotmail.com
    > www: http://pwp.maxis.net.my/winghoe
    > ---------------------------------------------------------------
    >
    >
    >
    >
    >
    >




  3. #3
    Lim Wing Hoe Guest

    Re: Calling an instantiated object's method from another object at the same level?

    What I meant by different level is that

    at the top level, MDI instantiates PublicChat, PublicWhiteBoard and
    EventsLog.
    at PublicChat level, it instantiates EventsLog.

    I have added the EevntsLog to MDI. However, when PublicChat instantiates
    another EventsLog, I'm not sure if I have created another object or not.

    However, I fear that I have just created another EventsLog object somewhere
    under the PublicChat object. If yes, I certainly don't want it to be in the
    MDI.

    If I export the EventsLog object in PublicChat and then add it to the MDI,
    wouldn't I end up with two copies of EventsLog objects?

    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    ICQ: 2213281
    Email: winghoe@hotmail.com
    www: http://pwp.maxis.net.my/winghoe
    ---------------------------------------------------------------


    "Paul Clapham" <pclapham@core-mark.com> wrote in message
    news:39f22f39@news.devx.com...
    > I don't know what this "level" stuff is, but your PublicChat class appears
    > to contain a private object of class EventsLog. Is this the same

    EventsLog
    > object that you have added to your MDI? Or did you create ANOTHER

    EventsLog
    > object somewhere else and add it to the MDI?
    >
    > If the PublicChat object is the one logging the events, then it needs to
    > export its EventsLog object so that it can be added to the MDI. Sample
    > code, if you don't already have something like this:
    >
    > /* in class PublicChat */
    > public EventsLog getEventsLog() {
    > return eventsLog;
    > }
    >
    > /* in main program */
    > desktop.add(yourPublicChatObject.getEventsLog());
    >
    > There are other solutions to this problem, such as providing a method to
    > pass an EventsLog object into the PublicChat object.







  4. #4
    Paul Clapham Guest

    Re: Calling an instantiated object's method from another object at the same level?

    When you execute "new EventsLog()" you are creating another object. From
    your description I don't know what you want to do so I can't really advise
    you.

    Lim Wing Hoe <winghoe@hotmail.com> wrote in message
    news:39f2648b@news.devx.com...
    > What I meant by different level is that
    >
    > at the top level, MDI instantiates PublicChat, PublicWhiteBoard and
    > EventsLog.
    > at PublicChat level, it instantiates EventsLog.
    >
    > I have added the EevntsLog to MDI. However, when PublicChat instantiates
    > another EventsLog, I'm not sure if I have created another object or not.
    >
    > However, I fear that I have just created another EventsLog object

    somewhere
    > under the PublicChat object. If yes, I certainly don't want it to be in

    the
    > MDI.
    >
    > If I export the EventsLog object in PublicChat and then add it to the MDI,
    > wouldn't I end up with two copies of EventsLog objects?
    >
    > --
    >
    >
    >
    > Best Regards,
    > Wing Hoe
    > ---------------------------------------------------------------
    > ICQ: 2213281
    > Email: winghoe@hotmail.com
    > www: http://pwp.maxis.net.my/winghoe
    > ---------------------------------------------------------------
    >
    >
    > "Paul Clapham" <pclapham@core-mark.com> wrote in message
    > news:39f22f39@news.devx.com...
    > > I don't know what this "level" stuff is, but your PublicChat class

    appears
    > > to contain a private object of class EventsLog. Is this the same

    > EventsLog
    > > object that you have added to your MDI? Or did you create ANOTHER

    > EventsLog
    > > object somewhere else and add it to the MDI?
    > >
    > > If the PublicChat object is the one logging the events, then it needs to
    > > export its EventsLog object so that it can be added to the MDI. Sample
    > > code, if you don't already have something like this:
    > >
    > > /* in class PublicChat */
    > > public EventsLog getEventsLog() {
    > > return eventsLog;
    > > }
    > >
    > > /* in main program */
    > > desktop.add(yourPublicChatObject.getEventsLog());
    > >
    > > There are other solutions to this problem, such as providing a method to
    > > pass an EventsLog object into the PublicChat object.

    >
    >
    >
    >
    >




  5. #5
    Lim Wing Hoe Guest

    Re: Calling an instantiated object's method from another object at the same level?

    Erm...ok....my objective is not to create another EventsLog() object. What I
    want is to let PublicChat() object that is created in the MDI to use methods
    and variables in the EventsLog() object that is created inside the MDI.

    My lecturer told me to create the EventsLog() object in the MDI and then
    pass the EventsLog() object into PublicChat() object. Will it work?

    ----------------------------------------------------------------------------
    ---
    public class MDI extends JFrame
    {
    private EventsLog eventsLog;
    private PublicChat publicChat;

    public MDI()
    {
    eventsLog = new EventsLog();
    publicChat = new PublicChat(eventsLog);
    }
    ......
    }
    ----------------------------------------------------------------------------
    ---
    public class PublicChat
    {
    private EventsLog eventsLog;

    public PublicChat(EventsLog eventsLog)
    {
    this.eventsLog = eventsLog;
    }
    ....
    }
    ----------------------------------------------------------------------------
    ---
    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    ICQ: 2213281
    Email: winghoe@hotmail.com
    www: http://pwp.maxis.net.my/winghoe
    ---------------------------------------------------------------


    "Paul Clapham" <pclapham@core-mark.com> wrote in message
    news:39f348f5@news.devx.com...
    > When you execute "new EventsLog()" you are creating another object. From
    > your description I don't know what you want to do so I can't really advise
    > you.





  6. #6
    Paul Clapham Guest

    Re: Calling an instantiated object's method from another object at the same level?

    Yes, that will work.

    Lim Wing Hoe <winghoe@hotmail.com> wrote in message
    news:39f46914@news.devx.com...
    > Erm...ok....my objective is not to create another EventsLog() object. What

    I
    > want is to let PublicChat() object that is created in the MDI to use

    methods
    > and variables in the EventsLog() object that is created inside the MDI.
    >
    > My lecturer told me to create the EventsLog() object in the MDI and then
    > pass the EventsLog() object into PublicChat() object. Will it work?
    >





  7. #7
    Lim Wing Hoe Guest

    Re: Calling an instantiated object's method from another object at the same level?

    Thanks Paul.

    I added the codes to the PublicChat constructor

    class PublicChat
    { private EventsLog eventsLog;

    public PublicChat(EventsLog eventsLog)
    {
    this.eventsLog = eventsLog;
    }
    }

    and in the main program,

    PublicChat publicChat = new PublicChat(eventsLog);

    it works.
    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    ICQ: 2213281
    Email: winghoe@hotmail.com
    www: http://pwp.maxis.net.my/winghoe
    ---------------------------------------------------------------


    "Paul Clapham" <pclapham@core-mark.com> wrote in message
    news:39f5993d$1@news.devx.com...
    > Yes, that will work.
    >
    > Lim Wing Hoe <winghoe@hotmail.com> wrote in message
    > news:39f46914@news.devx.com...
    > > Erm...ok....my objective is not to create another EventsLog() object.

    What
    > I
    > > want is to let PublicChat() object that is created in the MDI to use

    > methods
    > > and variables in the EventsLog() object that is created inside the MDI.
    > >
    > > My lecturer told me to create the EventsLog() object in the MDI and then
    > > pass the EventsLog() object into PublicChat() object. Will it work?
    > >

    >
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center