New buttons on-the-fly

Hello,



I was wondering if it is possible to create and destroy buttons on the fly without recreating the whole interface.



I want to make a kind of diagram editor. The user will be able to click on rectangle or circle icons in a palette to create new diagram blocks then drag and drop these blocks and remove them later. I would like these blocks to be nifty buttons.



thanks

Maybe thats similar to what you’r looking for.

There also exists a ButtonBuilder and some other. Check the JavaDoc or google for more info.

Does it really allow us to dynamically create new GUI items?

In all the examples I have seen, the structure of the GUI is always built using the class constructors, which makes them static structures.

you can get the rootElement from the mainScreen (using the example from the link) and from the rootElement u can get a List. and so on.

i think u can just add some buttons with those stuff. and if it doesn’t actualize itself and u don’t wanna recreate the interface (which is quite understandable) just think of a way around it.



example:

it isn’t the most elegant and reasonable way to do it but i think your interface can’t have too much buttons so create some in advance and set them invisible. if you need a new button just make them visible. never heard that too much buttons are making a performance difference(most of the time it’s the functions called by the buttons). and 10mb ram for at least 50 buttons (just guessing these values) shouldn’t be much of a problem today.

You can dynamically add elements to Nifty at runtime. For your editor I think you should take a look at the drag and drop example that is a part of the nifty-examples project. It’s in the “de.lessvoid.nifty.examples.dragndrop” and there are buttons in there that create new elements (boxes you can drag around) when you click on them :slight_smile:



There are two general ways how you can do that:


  1. You can use a specific *Builder class for the element/control you want to create and use the builder methods to set the attributes of the new element. When you finally call *Builder.build() you need to specify the Nifty instance, the Screen instance and the Parent element you want that new element to be a child of. *Builder.build() will create the Element dynamically.


  2. You can use the specific *Create classes if you don’t like the Builder way. The Create classes have ordinary java setters to set the attributes and finally you call .create() on that class which takes the same parameters as the Builder.build() method.



    Here is an example which I just copied directly from the drag and drop example I’ve mentioned above:



    [java] public void spawnDraggable() {

    DraggableBuilder builder = new DraggableBuilder() {{

    width(“120px”);

    height(“120px”);

    backgroundColor(randomColor());

    childLayoutCenter();

    valignTop();

    text(new TextBuilder() {{

    text(“Drag Me!”);

    style(“descriptionText”);

    }});

    }};



    Element draggables = screen.findElementByName(“draggables”);

    builder.build(nifty, screen, draggables);

    }



    public void spawnWindow() {

    Element windows = screen.findElementByName(“windows”);



    String windowId = NiftyIdCreator.generate() + 1000;

    CreateWindow windowAttributes = new CreateWindow(“window-” + windowId, “New Window [” + windowId + “]”);

    windowAttributes.setWidth(“360px”);

    windowAttributes.setHeight(“240px”);

    windowAttributes.create(nifty, screen, windows);

    }[/java]



    The spawnDraggable() method uses the builder stuff to create a new element and the spawnWindow() method uses the Create
    method. :slight_smile: