[Solved] Altering Tabs in Nifty GUI

I’m having trouble with Nifty GUI. Seems simple, but I’m probably missing something. I have a button and a group of two tabs. When I press the button, I want to change the caption on the tabs. However, I can’t seem to get it to work.



Here’s my XML (BTW, I had to gut it a bit to have it show in the post, is there a better way?):



nifty>

useControls filename=“nifty-default-controls.xml”/>

useStyles filename=“nifty-default-styles.xml”/>

screen id=“start” controller=“mygame.GuiScreenController”>

layer id=“main” backgroundColor="#0000" childLayout=“vertical”>

panel id=“button_panel” width=“100%” height=“30%” childLayout=“vertical” backgroundColor="#00f8" visibleToMouse=“true”>

control name=“button” label=“A Button” id=“aButton” align=“center” width=“100%”>

interact onRelease=“buttonClicked()”/>

/control>

/panel>

panel id=“tab_panel” width=“100%” height=“40%” childLayout=“horizontal” backgroundColor="#88f8" visibleToMouse=“true”>

control id=“tab_control” name=“nifty-tabs” width=“100%” height=“100%” buttonWidth=“50%” buttonHeight=“10%”>

control id=“tab_1” name=“nifty-tab” caption=“tab 1” childLayout=“vertical”>

control height=“100%” width=“100%” align=“center” valign=“center” name=“label” text=“Tab #1”/>

/control>

control id=“tab_2” name=“nifty-tab” caption=“tab 2” childLayout=“vertical”>

control height=“100%” width=“100%” align=“center” valign=“center” name=“label” text=“Tab #2”/>

/control>

/control>

/panel>

/layer>

/screen>

/nifty>





My Main.java class:



public class Main extends SimpleApplication

{

private NiftyJmeDisplay m_display;

public static void main(String[] args)

{

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp()

{

setDisplayStatView(false);

getFlyByCamera().setDragToRotate(true);

getInputManager().setCursorVisible(true);



m_display = new NiftyJmeDisplay(getAssetManager(), getInputManager(), getAudioRenderer(), getGuiViewPort());

getGuiViewPort().addProcessor(m_display);

m_display.getNifty().addXml(“Interface/main.xml”);

m_display.getNifty().gotoScreen(“start”);

}

}





And my controller class:



public class GuiScreenController implements ScreenController

{

private Screen m_screen;

private TabControl m_tab1;

private TabControl m_tab2;



public GuiScreenController()

{

}



public void buttonClicked()

{

m_tab1.setCaption(“A”);

m_tab2.setCaption(“B”);

}



public void bind(Nifty nifty, Screen screen)

{

m_screen = screen;

m_tab1 = m_screen.findControl(“tab_1”, TabControl.class);

m_tab2 = m_screen.findControl(“tab_2”, TabControl.class);

}



public void onStartScreen()

{

}



public void onEndScreen()

{

}

}





Thanks!

I still haven’t figured out what is wrong with the first post, so I’m trying to use layers and switching between the layers. It’s throwing an exception on the second tabs control. If I comment out the first tabs control, the second comes up fine.



Main.java is same as above.



Exception:



WARNING: Exception thrown by;EventService topic subscriber:de.lessvoid.nifty.Nifty$ClassSaveEventTopicSubscriber@78d88d3e{tab_control2}{de.lessvoid.nifty.controls.tabs.TabsControl@51211f6d}{class de.lessvoid.nifty.elements.events.ElementShowEvent}. Subscriber class:class de.lessvoid.nifty.Nifty$ClassSaveEventTopicSubscriber

org.bushe.swing.exception.SwingException: Exception handling event event class=none, event=null, topic=tab_control2, eventObj=de.lessvoid.nifty.elements.events.ElementShowEvent@70ee5558

org.bushe.swing.exception.SwingException: Exception handling event event class=none, event=null, topic=tab_control2, eventObj=de.lessvoid.nifty.elements.events.ElementShowEvent@70ee5558

at org.bushe.swing.event.ThreadSafeEventService.handleException(ThreadSafeEventService.java:2021)

at org.bushe.swing.event.ThreadSafeEventService.onEventException(ThreadSafeEventService.java:1999)

at org.bushe.swing.event.ThreadSafeEventService.publish(ThreadSafeEventService.java:982)

at org.bushe.swing.event.ThreadSafeEventService.publish(ThreadSafeEventService.java:920)

at de.lessvoid.nifty.Nifty.publishEvent(Nifty.java:193)

at de.lessvoid.nifty.elements.Element.internalShow(Element.java:1246)

at de.lessvoid.nifty.elements.Element.internalShow(Element.java:1243)

at de.lessvoid.nifty.elements.Element.show(Element.java:1233)

at mygame.GuiScreenController.onStartScreen(GuiScreenController.java:55)

at de.lessvoid.nifty.screen.Screen.onStartScreenHasEnded(Screen.java:712)

at de.lessvoid.nifty.screen.Screen$StartScreenEndNotify.perform(Screen.java:635)

at de.lessvoid.nifty.screen.Screen$LocalEndNotify.perform(Screen.java:611)

at de.lessvoid.nifty.screen.Screen.startLayers(Screen.java:268)

at de.lessvoid.nifty.screen.Screen.startScreen(Screen.java:217)

at de.lessvoid.nifty.Nifty.gotoScreenInternal(Nifty.java:618)

at de.lessvoid.nifty.Nifty.gotoScreen(Nifty.java:586)

at mygame.Main.simpleInitApp(Main.java:36)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:225)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)

at java.lang.Thread.run(Thread.java:722)

Caused by: java.lang.NullPointerException

at de.lessvoid.nifty.controls.tabs.TabsControl.onEvent(TabsControl.java:198)

at de.lessvoid.nifty.controls.tabs.TabsControl.onEvent(TabsControl.java:28)

at de.lessvoid.nifty.Nifty$ClassSaveEventTopicSubscriber.onEvent(Nifty.java:1402)

at org.bushe.swing.event.ThreadSafeEventService.publish(ThreadSafeEventService.java:980)

… 18 more





XML:



?xml version=“1.0” encoding=“UTF-8”?>

nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>

useControls filename=“nifty-default-controls.xml”/>

useStyles filename=“nifty-default-styles.xml”/>

screen id=“start” controller=“mygame.GuiScreenController”>

layer backgroundColor="#0000" childLayout=“vertical”>

panel id=“button_panel” width=“100%” height=“30%” childLayout=“vertical” backgroundColor="#00f8" visibleToMouse=“true”>

control name=“button” label=“Panel 1” align=“center” width=“100%”>

interact onRelease=“buttonClicked(1)”/>

/control>

control name=“button” label=“Panel 2” align=“center” width=“100%”>

interact onRelease=“buttonClicked(2)”/>

/control>

/panel>

/layer>



layer backgroundColor="#0000" childLayout=“vertical”>

panel width=“100%” height=“30%”/>

panel id=“panel_1” width=“100%” height=“40%” childLayout=“horizontal” backgroundColor="#88f8">

control id=“tab_control1” name=“nifty-tabs” width=“100%” height=“100%” buttonWidth=“50%” buttonHeight=“10%”>

control id=“abba” name=“nifty-tab” caption=“tab 1”/>

/control>

/panel>

/layer>



layer backgroundColor="#0000" childLayout=“vertical”>

panel width=“100%” height=“30%”/>

panel id=“panel_2” width=“100%” height=“40%” childLayout=“horizontal” backgroundColor="#88f8">

control id=“tab_control2” name=“nifty-tabs” width=“100%” height=“100%” buttonWidth=“50%” buttonHeight=“10%”>

control id=“dabba” name=“nifty-tab” caption=“tab 3” />

/control>

/panel>

/layer>

/screen>

/nifty>





and the ScreenController:





public class GuiScreenController implements ScreenController

{

private Screen m_screen;

private Element m_panel1;

private Element m_panel2;



public GuiScreenController()

{

}



public void buttonClicked(String panel)

{

switch (panel)

{

case “1”:

m_panel1.show();

m_panel2.hide();

break;

case “2”:

m_panel1.hide();

m_panel2.show();

break;

}



System.out.println("Button Clicked: " + panel);

}



public void bind(Nifty nifty, Screen screen)

{

m_screen = screen;

m_panel1 = m_screen.findElementByName(“panel_1”);

m_panel2 = m_screen.findElementByName(“panel_2”);

}



public void onStartScreen()

{

m_panel1.hide();

m_panel2.show();

}



public void onEndScreen()

{

}

}

Well, looks like Nifty 1.3.2 solved it. I thought I had downloaded it, but apparently not.

Good to know. :slight_smile: