Dynamic Nifty Buttons in Java

Is there a way to change the background image of a Nifty button in Java or do I need to create a new button and add it? I see methods for changing the button text, but not the background image.

It looks the background image of a button is a panel, then you have to retrieve this panel, and change its image.

I changed the button to an image and put a text line under it. It’s much easier to change the image on an image than a button by using the ImageRenderer.



[java]

if (screen.getScreenId().equals(“introScreen2”)) {

for (int i=0; i<app.getLevelFileNames().size(); i++) {

LevelName levelName = app.getLevelFileNames().get(i);



// find the image element

Element imageElement = screen.findElementByName(“imageLevel” + String.valueOf(i));

// get the ImageRenderer

ImageRenderer imageRenderer = imageElement.getRenderer(ImageRenderer.class);

// change the image

imageRenderer.setImage(nifty.getRenderEngine().createImage(“Interface/Nifty/Images/unlocked.png”, false));



// find the text element

Element textElement = screen.findElementByName(“labelLevel” + String.valueOf(i));

// get the TextRenderer

TextRenderer textRenderer = textElement.getRenderer(TextRenderer.class);

// change the text

textRenderer.setText(levelName.displayName);

// resetLayout to get the new text wrapping to display correctly

textRenderer.resetLayout(textElement);



}

// resetLayers to get the lower elements to move down due to new text wrapping on items above

screen.layoutLayers();

}



[/java]