Nifty GUI uses XML to store the layout of your GUI. To connect such a XML description with some class in Java there is the ScreenController Interface you need to provide:
/**
* ScreenController Interface all screen controllers should support.
* @author void
*/
public interface ScreenController {
/**
* Bind this ScreenController to a screen. This happens
* when the Screen got the onStartScreen() method.
* @param nifty nifty
* @param screen screen
*/
void bind(Nifty nifty, Screen screen);</p>
/**
* called when all start effects are ended and the screen
* is ready for interactive manipulation.
*/
void onStartScreen();
/**
* called when the onEndScreen effects ended and this screen is done.
*/
void onEndScreen();
}</pre>
To let Nifty know what ScreenController class you want to use for a Nifty Screen there is the "controller" attribute on the xml tag:</p>
...
...
</screen>
</nifty></pre></p>
To resolve the concrete ScreenController instance Nifty can use two different ways:
- Nifty creates a new instance of the given ScreenController class and registers this instance with the Screen</li>
- You can give Nifty a ScreenController instance that matches the class given in the controller attribute. Nifty will first look for an existing instance and creates a new class only when it can't find one.</li>
</ol></p>
To register ScreenController instances with Nifty there are additional parameters on the "fromXml()" method of the Nifty class. This way you can even add multiple different instances for use in multiple Nifty screens.
/**
* load xml.
* @param filename file to load
* @param controllers controllers to use
*/
public void fromXml(final String filename, final ScreenController ... controllers) {
...</pre></p>
Note: </strong>You still need the controller attribute in the xml so that Nifty can connect the screen with your ScreenController instance.</p>
Note: </strong>In case you use anonymous inner classes like in this example here:</p>
class MyStuff {
...
nifty.fromXml("menu.xml", new ScreenController() {
public void bind(Nifty nifty, Screen screen) {
...</pre></p>
the classname then turns to "MyStuff$1".