Nifty GUI - Best Practices

This page is a short list of best practices that you should know of when starting to use Nifty GUI. The JME3 tutorials focus on JME3-Nifty integration related details. You will find more features in the Nifty GUI Manual.

XML or Java?

You can build Nifty GUIs using XML or Java syntax. Which one should you choose? The XML and Java syntax are equivalent, so is it an either-or choice? Not quite. You typically use XML and Java together.

  • Build your basic static UI layout using XML - it’s cleaner to write and read.

  • Use Java syntax to control the dynamic parts of the GUI at runtime - it’s easier to interact with object-oriented code.

  • Example: You design two UIs with slightly different XML layouts for mobile and desktop. If you use the same IDs for equivalent elements, your dynamic Java code works the same no matter which of the two base XML layout you use it on. This allows you to switch between a phone and a desktop UI by simply swapping one base XML file.

  • When you open an XML file, you can switch between XML Editor and GUI Preview mode.

Validate the XML before loading

The Nifty class has validateXml() method that takes the same input XML argument as fromXml(). Nifty does not validate the Xml by default, and will blow up in surprising ways if your XML does not conform to the schema. Adding the validation step will save you debugging time. You can validate right before loading, or in your unit tests.

Use Code Completion

  • Include the following XML schema in the first line of your NiftyGUI XML files

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="" xmlns:xsi="" xsi:schemaLocation="">
     <!-- Your IDE now tells you that one <screen></screen> element is expected here, etc. -->
  • Now your IDE (including the jMonkeyEngine SDK) will display extra info and do code completion for the Nifty GUI API.

Use the ID String Right

  • If you want to interact with an element, give it an ID (String).

  • Use transparent ID-less panels as anonymous spacers.

Sizing Layers and Panels

  • Specify widths and heights in percent to allow the GUI to scale.

  • Use * instead of a fixed value to make the element fill the remaining space automatically.

  • Be cautious when specifying fixed sizes, and test the outcome in various resolutions.

Colorcode for Clarity

Screens, layers, and panels…

  • … can have an RGBA background color.
    You can use temporary colors during the design phase to highlight which container is which.

  • … can be transparent.
    In the finished GUI, screens, layers, and panels are typically transparent; the visible elements are the images, text fields, and controls, inside the panels.

During development (and during a tutorial), the following debug code makes all panels visible. This helps you arrange them and find errors.


Before the release, and during testing, set the debug view to false again.