Nifty: how to hide an image from java

does ‘hide()’ work on images?
It looks to me, that it only works on panels (and layers?).


Or can someone help me, what should I do in java to hide an image by code inside nifty?

Yes it should work also for images , Indeed in my editor works fine . Are you sure that the id is valid ? I mean it actually points to that image ? .

Yup I use the same id in the same function to change the subimage, so I don’t think the id is the prob.

[java] public void setImage(String id, Texture2D t, String type, int dx, int dy, int images, int fields) {
// get element and NiftyImage
String Nr[] = id.split(",");
for (int i = 0; i < images; i++) {
Element element = nifty.getCurrentScreen().findElementByName(type + i);
Debug.text("**setImage: “+type+id+” "+fields);
NiftyImage image = element.getRenderer(ImageRenderer.class).getImage();

        if (Nr.length &gt; i * fields) {
            int counter = Integer.parseInt((String) Nr[i * fields]);
            Debug.text(type + counter+" "+i);

    // change the parameters of the imageMode
            image.getImageMode().setParameters("subImage:" + (counter * dx) + ",0,"+dx+","+dy);
        } else {


}  [/java] 

(I set the image to 0,0,0,0 as a workaround now, instead of hiding it totally, but that is not that nice.)

Maybe something in my xml is wrong.
Do you have a short example (java and xml?) where it works for you?

Sure , but I’m afraid , I don’t have an explanation about why it doesn’t work for you :open_mouth: .

Java class :

import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class Main extends SimpleApplication implements ScreenController {

public static void main(String[] args) {
    Main app = new Main();
private Nifty nifty;

public void simpleInitApp() {

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
    nifty = niftyDisplay.getNifty();
    nifty.fromXml("Interface/showhow.xml", "GScreen0", this);

    // attach the nifty display to the gui view port as a processor
    //disable the fly cam


public void bind(Nifty nifty, Screen screen) {
    System.out.println("bind( " + screen.getScreenId() + ")");

public void onStartScreen() {

public void onEndScreen() {

public void toggle(){
    Element image = this.nifty.getCurrentScreen().findElementByName("GImage1");


Xml gui file :
<screen id=“GScreen0” controller=“mygame.Main”>
<layer id=“GLayer0” childLayout=“center”>
<panel id=“GPanel0” height=“224px” style=“nifty-panel-simple” width=“286px” childLayout=“vertical” y=“156” x=“307”>
<image id=“GImage1” height=“125px” width=“153px” align=“center” filename="./Interface/Leaf.png"/>
<control name=“button” id=“GButton0” label=“show/hide” height=“86px” align=“center” width=“228px” childLayout=“center”>
<interact onClick=“toggle()”/>
<useControls filename=“nifty-default-controls.xml”/>
<useStyles filename=“nifty-default-styles.xml”/>

I hope it helps! :slight_smile:

Thank you very much for your example!

I was able to find my nifty-beginner-bug now.
Made the ‘flaw’ that I:

[java] setUnits(DataModel.vArmies.get(Integer.parseInt(Nr[1])));



If I change it in simple order that I .show() the unset Images first and then set them or hide where there are empty (which looks first from a coding point a bit strange), my code works as suggested.

Suppose its the pitfall that .hide() only works if elements are before visible.

You helped me very much.

You are welcome :slight_smile: . I think it is fault of this bug : . In nifty-gui 1.4 is solved by the way :slight_smile: .