How can I access object method inside other object?

Within DPanel I have added two objects, captionBar and btnClose and have called their display() methods successfully as shown in DPanel.display(). I have attempted to bring in a third object via the DPanel method addObject. However I get the message that display() does not exist… referring to the Existing_Object.display().

The Existing_Object has a display() method no different to the two objects within DPanel. I therefore assume that the invisibility is caused by my attempt to ‘import’ a pre-existing object to make it part of DPanel. Can anybody please explain, simply, what it is that I don’t understand. Thank you.

class DPanel {
//variable declarations
    ...
    PFont arial14;
    Object ExistingObject;

//Constructor
    DPanel(int _x, int _y etc.) {

    btnClose = new DButton(x, y, etc.);
    captionBar = new DLabel(x, y, etc.);

  }//end constructor

//Methods
    void addObject(Object _Existing_Object) {
    Existing_Object = _Existing_Object;
}// end addObject

  void display() {
    fill(0);
    text(txt, etc...);
    captionBar.display();
    btnClose.display();
    Existing_Object.display();
  }// end display

}//end DPanel
1 Like

The problem is that you have declared ExistingObject to be of type Object which is the base class for all Java and user-defined classes. So in the statment Existing_Object.display(); will look for a display method inside the class Object and will not find one.

1 Like

Create an Interface like this

public Interface Displayable {
  public void display();
}

then declare the DPanel class

public class DPanel implements Displayable {

PFont arial14;
Displayable ExistingObject;

//Constructor
DPanel(int _x, int _y etc.) {

btnClose = new DButton(x, y, etc.);
captionBar = new DLabel(x, y, etc.);

}//end constructor

  //Methods
  void addObject(Displayable _Existing_Object) {
    Existing_Object = _Existing_Object;
  } // end addObject

  void display() {
    fill(0);
    text(txt, etc…);
    captionBar.display();
    btnClose.display();
    Existing_Object.display();
  } // end display

} //end DPanel

All your GUI control class (e.g. DButton, DLabel etc) should also implement Displayable

1 Like

Thank you for your very quick reply. I have attempted to respond via the forum but the web page is constantly blank.
Will you please be kind enough quark, to suggest what I should do instead. This is not just a mistake on my part, I actually do not know even if it is possible to add an object this way nor how.
Thank you

I have - see my second post!

Maybe the web site goes off at night, it is back now :slight_smile:
Many thanks quark for the introduction to something completely unknown to me. I will now play for a while and hopefully succeed