Nifty GUI: how to add a custom xml defined control dynamically?

Hi all,

This is the problem: I have a screen and some custom controls defined in xml files. That’s okay. Now I want from java to take one panel of this screen in order to add there one of my custom controls. Is there a way to do that without creating a new control dynamically (the builders)? I mean, can I get or lad the xml custom control and then add it in this panel?

Thanks

What do you mean by that ?
You can remove objs and create new ones in the nifty at run time, but its not clear what you want.

You can use this to construct your custom controls via code:

new ControlBuilder

Thanks for your help and sorry for my english. I will try to explain it better.

I have a gui defined in a xml file, ‘gui.xml’, like this…

<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=" …>

<useControls filename=“custom_control.xml” />


<panel id=“panel1” width=“100%” height=“75%” childLayout=“horizontal”>
“here comes the control”

I know how to include and use this custom control within this xml file (gui). What I want to do now is to iclude this custom control dynamically (in java code) in one of the gui elements (for example in a ‘panel2’). Is there a way to call this control (custom_control.xml) dynamically? Or should I create a new one in java (with the builders)?

So you defined a custom control in xml and want to instantiate it in java?
We do that too. Here’s how :smile:

You create your own Builder and chain your parameters through to the nifty control you are creating.
You pass the control name, as defined in your custom control, in the super constructor.

Heres an example of one of ours, where you can see how we pass the parameters to the control.

private class EquipSlotBuilder extends ControlBuilder {
    private EquipSlotBuilder(int x, int y, String iconName, EquipmentSlot equipSlot) {
        super(NiftyConstants.EQUIP_SLOT_CONTROL_NAME);
        id(NiftyConstants.ITEM_DROPPABLE_PREFIX + ":" + equipSlot.toString());
        parameter(NiftyConstants.EQUIP_SLOT_ATTRIBUTE, equipSlot.toString());
        parameter(NiftyConstants.SLOT_ATTRIBUTE, NiftyConstants.ITEM_DROPPABLE_PREFIX + ":" + equipSlot.toString());
        x(String.valueOf(x));
        y(String.valueOf(y));
        imageMode(getIconNiftyImageMode(iconName));
        filename(getIconNiftyFileName(iconName));
    }
}
2 Likes

Thank you all. I will try this.

1 Like