Two new nifty interaction snippets added to the wiki. Change image, change interaction

I’ve added two snippets of information on how to interact with nifty.

One is about changing the image of an element, which is taken from the official nifty blog, but i thought it could be nice to have in jme3’s wiki too.

The second is investigated by myself, on how to change the interaction element.

You can also alter the appearance and functions of your nifty elements from java.

Here’s an example of how to change an image:

[java]NiftyImage img = nifty.getRenderEngine().createImage(“Interface/Images/image.png”, false);

Element niftyElement = nifty.getCurrentScreen().findElementByName(“myElement”);


To change the onClick() event of an element:

[java]Element niftyElement = nifty.getCurrentScreen().findElementByName(“myElement”);

niftyElement.setInteraction(new ElementInteraction(nifty) {


public void onClick() {

// call java functions normally.





public boolean onClick(MouseInputEvent inputEvent) {


return super.onClick(inputEvent);



For this to work, there already needs to be an tag inside your xml element:



Just a head’s up here.

the “findElementByName()” method here will look for “myElement” in IDs among the controls and panels and layers. It will NOT search in the “name” tags.

So, if you have something defined like so:


⪪label id=“hint_text” name=“text” font=“aurulent-sans-17.fnt” color="#ffff" text=“Please make a selection by clicking on the appropriate button.” align=“center” valign=“center”>


and your method looks for “text”, it will not work. It only will find “hint_text” for that element.

So, to resume, that method should be called findElementByID.

Among all the examples I have found I often found the name=“somename”, so I naturally thought this method would look for name tags, as it happens in CSS. Maybe using both would be better? findElementByID() and findElementByName() each looking for its proper tag.

Anyway. Hopefully that might help someone.

One more thing to the onclick-Event:

You can also change the Method that is called when the Element is clicked:

[java]Element el = nifty.getCurrentScreen().findElementByName(“myElement”);

NiftyMethodInvoker invoker = new NiftyMethodInvoker(




el.getInteraction().setOnClickMethod(invoker, true);[/java]

Maybe someone thinks it’s helpful :wink:

1 Like

I don’t know if it’s related, but you can use AssetCache when image isn’t on filesystem:


private void setNiftyImage(Element niftyElement,BufferedImage b) {

TextureKey key =new TextureKey(“textures/Monkey.jpg”,false);

Texture tex = assetManager.loadTexture(key);



AWTLoader loader =new AWTLoader();

Image imageJME=null;

imageJME = loader.load(b, true);


((DesktopAssetManager)assetManager).addToCache(new AssetKey(“pippo”), tex);

NiftyImage img = nifty.getRenderEngine().createImage(“pippo”, false);