Nifty: how to hide an image from java

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

[java]nifty.getCurrentScreen().findElementByName(“unit”+i).hide();[/java]

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 {
                  nifty.getCurrentScreen().findElementByName(type+i).hide();
                  image.getImageMode().setParameters("subImage:0,0,0,0");


        }
        //nifty.getCurrentScreen().findElementByName("unit1").hide();

    }
}  [/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 :

[java]import com.jme3.app.SimpleApplication;
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();
    app.start();
}
private Nifty nifty;

public void simpleInitApp() {

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                                                      inputManager,
                                                      audioRenderer,
                                                      guiViewPort);
    
    nifty = niftyDisplay.getNifty();
    System.out.println(nifty.getVersion());
    nifty.fromXml("Interface/showhow.xml", "GScreen0", this);

    // attach the nifty display to the gui view port as a processor
    guiViewPort.addProcessor(niftyDisplay);
    
    //disable the fly cam

//
flyCam.setEnabled(false);
flyCam.setDragToRotate(false);
//
inputManager.setCursorVisible(true);
}

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

public void onStartScreen() {
    System.out.println("onStartScreen");
}

public void onEndScreen() {
    System.out.println("onEndScreen");
}



public void toggle(){
    Element image = this.nifty.getCurrentScreen().findElementByName("GImage1");
    if(image.isVisible()){
        image.hide();
    }else{
        image.show();
    }
        
}

}[/java]

Xml gui file :
[xml]<nifty>
<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()”/>
</control>
</panel>
</layer>
</screen>
<useControls filename=“nifty-default-controls.xml”/>
<useStyles filename=“nifty-default-styles.xml”/>
</nifty>[/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])));

               nifty.getCurrentScreen().findElementByName("unitpanel").show();
                nifty.getCurrentScreen().findElementByName("buildingspanel").hide();

[/java]

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 : https://github.com/void256/nifty-gui/issues/43 . In nifty-gui 1.4 is solved by the way :slight_smile: .