A few questions about Nifty 1.3

Hello. With Nifty 1.3 a few things have changed, I was wondering if anyone a bit more experienced with it could provide some tips.

With 1.2, I was working on making a chatbox. I’ve converted it over to 1.3 and it works just fine. I was planning to add channels though, and as a means of helping users differentiate the channels, I was hoping to give each channel a specific coloring. In Nifty 1.2, I was directly adding labels to the listbox component, which would have allowed me to change colors quite easily. I’m not sure how to approach this in 1.3, since the Listbox just takes in a Model class. One interesting recommendation was to append something like “#FF0000#” to a string, which does change text color in a textfield, but it seems to have no effect in the listbox. Well. That is not entirely true-- “#FF0000” would disappear, but the text color wouldn’t change in that case. Would anyone have any suggestions for how I might go about adding color to text in a listbox?

Also, one of the features of a chatbox that my partner would like to see implemented is message submission upon pressing enter when focus is on the textfield. I heard this was implemented in Nifty 1.3-- perhaps I was wrong?

[xml] <control id=“chat_text” name=“textfield” valign=“center” width=“90%”>

<!-- Does nothing, at the moment. -->

<interact onEnter=“sendChat()”/>


Here is a simple textfield, basically a copy/paste of the one I’m using in the project. Is there any reason this wouldn’t work, or is the onEnter interact just not implemented for the textfield yet? If the latter, is there any way I can get it to work the way I want it to?

You can give the ListBox your own implementation of the ListBox.ListBoxViewConverter interface. Nifty will call the display() method with the Element and the Item it needs to display in the ListBox. Your job would then be to display the given item on the given Element. In its basic version (see the interface de.lessvoid.nifty.controls.ListBox.java for an example implementation) it’s just updating the TextRenderer of the element. This would be your chance to change the text color too :slight_smile: Probably storing some information on your item to define the color for the item.

The second part of your question is even easier now with 1.3 … at least when you have a basic understanding of the EventBus stuff. I still need to blog about how exactly it works but for the moment you can simply add a method like this to your ScreenController class:


public void onMyTextFieldInputEvent(final String id, final NiftyInputEvent event) {

if (NiftyInputEvent.SubmitText.equals(event)) {

// enter has been pressed on the textfield … do something



And then Nifty will AUTOMAGICALLY ^^ call this method with all input events the textfield receives. The preview demo on the Nifty blog demonstrates how this works too.

And before people like madjack :stuck_out_tongue: complain again … I KNOW that this needs documentation in the wiki and on the blog! Please be patient! :slight_smile:

In the meantime you should svn check out the source of the preview demo to learn how this works (https://nifty-gui.svn.sourceforge.net/svnroot/nifty-gui/nifty-default-controls-examples/trunk/).

Good luck :slight_smile:

Wondering if being mentioned is a good or bad thing in that context. :stuck_out_tongue:

I’ll admit that though Void, the doc is much better than it was. It is still lacking but it’s coming along. We’ll get through it, we will. :stuck_out_tongue:

I’m currently also working on a chat area control. This will have the “enter to chat” functionality already incorporated as well as callback methods to send / receive chat messages.

Currently, multiple channels are not planned. But you could easily implement that yourself by hiding and displaying the chat areas for each channel.

Oh, you are also more then welcome to help out with the development of the chat area control off course, just wait for me to have a first alphe release, which is planned late this week, early next week. Then you can bug test and request additional features all you want.

You guys are always so quick to respond. It makes me feel sort of bad when I check back quite a while later. :stuck_out_tongue:

Anyway, thanks for all of the information. I was able to set it all up with relative ease thanks to that. :slight_smile:


To be honest, the custom ListBoxViewConverter is sort of a ripoff of ListBoxViewConverterSimple. It was such a simple change that I didn’t really need to do much. I was playing with it, and like you mentioned, went to set the color of the TextRenderer. I was expecting something similar to the RGB input used elsewhere, but it just gives the option of BLACK, WHITE, or NONE. Since I’m pretty new to working with Nifty, I thought that might have meant I was limited to those colors. I sort of stumbled upon the solution though. I was just playing with it, and set the color white. Apparently the “#FF0000#” I appended to the message earlier only takes effect when the TextRenderer’s color is set to white, because the color changed just like I wanted it to. It works now, so all I need to do is append the desired color to the string.

It is probably my inexperience speaking, but why not just have a function that takes in an RGB input for the color? Or is there perhaps a means of doing so that I’ve overlooked?


That sounds interesting, but I should probably stick to using my own code. This is a component of a course project, and I’d hate to throw away any of the work I’ve done-- it might make things easier, but then I’d have less to present as my own!

That said, sure. I don’t see why I couldn’t look over it and nag you with suggestions. :stuck_out_tongue:

if you are referring to setColor(Color color) of the TextRenderer then there really are lots of constructors available to create Color objects! There are all sorts of ways to get to a Color:

[java] /**

  • Create a color from a color String formated like in html
  • code but with alpha, e.g.: "#ff00ffff".
  • @param color the color string


    public Color(final String color);

  • Create a color from components.
  • @param newRed red component
  • @param newGreen green component
  • @param newBlue blue component
  • @param newAlpha alpha component


    public Color(final float newRed, final float newGreen, final float newBlue, final float newAlpha);

  • Create a color from another color, using the given alpha value.
  • @param newColor color
  • @param newAlpha alpha component


    public Color(final Color newColor, final float newAlpha);[/java]

    but maybe I’ve just misunderstand you? o_O

Nope, you understood perfectly. Wow. That was really simple, I can’t believe I missed that. :stuck_out_tongue: