Reusing common UI elements with frames

I’m trying to follow the advice I’ve seen in this forum that using frames is the way to go if we want reuse common components.

For example I have a UI element combination that I’m using in multiple places. It has two button components. I’ve wrapped these components in a Frame and I want to use this frame in a dialog that I open with via openWindow() method.

This all works fine however, I’m stuck when it comes to how to obtain a reference to the parent window. Occasionally I need to access some property, method, etc. of the enclosing window or (frame) from the child frame.

I can call getParent() which returns type Component. I can cast that to Window but that seems unreliable as I might have frames nested multiple levels deep.

Also is there a way to pass parameters to the child frame upon initialization?

I figured out the answer to my own question. I was thinking of things backwards. It’s quit easy access the child frame from the parent and set properties on the frame as required.

Hi - I’m trying something likewise but I would like to pass an object to the frame and have it used for intialisation. Something like a @WindowParam. However, I do not see any option to pass this as a parameter.

Could you maybe explain what you did to pass the property from the parent to the child frame?

Regards, Berend

Hi Berend

I’m a bit of a new comer to CUBA and revisiting Java after many years of doing Ruby & Javascript exclusively. I say only to caution that my solution might not be what someone more familiar with CUBA might do.

That being said, after a lot of banging my head on the wall, I realized that nothing about using a frame component prevented me from adding my own public interface.

In my case I just declared a private variable and provide a public setter method.

public class PanelNavigator extends AbstractFrame {
  private Object something;

  public void setSomething(Object aThing){
    something = aThing
  }

 public void activate(){
    something.doIt();
 }
}

From there I was able to manipulate the the frame as needed. For example my activate method in the example.

Also if whatever you’re doing can be done in the init method of the frame, it appears params of the of the Parent window are passed into the init method of the child frame.

Hope this helps. Happy to provide more clarification or samples if needed.

2 Likes

Hi Herby,

Thanks for pointing out. I missed the third argument on openFrame() that allows to pass parameters. That fixed it:

        Map<String, Object> params = new HashMap<>();
        params.put("category", c);
        Frame f = openFrame(null, "category-frame", params);
        ImplementationTable.add(f);
1 Like