[Nifty] Multiline label -- Solved


Here is another question :

I try to make some GUI with international labels.

In some language, the text stays on one line.

In others, the line is not wide enough and the traduction must be displayed on two lines

I have a XML :


<!-- Current description -->

<panel height=“100%” width=“75%” align=“center” valign=“center” childLayout=“vertical” visibleToMouse=“true” >

<image id=“imgCurrentDescription” height=“50%” width=“100%” filename=“empty.png” imageMode=“normal” />

<panel height=“5%” />

<label id=“txtCurrentDescription” height="*" width=“100%” font=“aurulent-sans-17.fnt” text="" align=“left” valign=“top” wrap=“true” />



I’ve found an example of a scrollpane on nifty wiki : http://sourceforge.net/apps/mediawiki/nifty-gui/index.php?title=ChatArea_Example

But it’s a little too complex for what I want to do. ( I don’t want to create my own controls yet )

So, is there a way to create a simple control pane wich display a wrapped text?

Thanks in advance

My label txtCurrentDescription is filled by code during the method onStartScreen :


//Init presentation

element = screen.findElementByName( “txtCurrentDescription” );

element.getRenderer( TextRenderer.class ).setText( LanguageManager.getGameMessage( “startKurfiirDescription” ) );


The text is too wide and don’t stay inside the box defined by the element

I haven’t been able to confitm it, yet, but i’ve found this method:


It sounds like it should do that, at least.

pre type="xml"
<label text=“My very long text” wrap=“true”/>

Tanks for the answers.





doesn’t seem to work in my case. ( Perhaps because my text is very long and contains ‘& #13;& #10;’

The attribute wrap seems to work bu only if I don’t edit my text after the first initialization. :frowning:

Another solution would be to but it inside a scroll pane.

Has anyone an example of how we can create a simple scroll pane?

didialchichi said:
Tanks for the answers.

doesn't seem to work in my case. ( Perhaps because my text is very long and contains '& #13;& #10;'

The attribute wrap seems to work bu only if I don't edit my text after the first initialization. :(

I've the same behaviour
I used WordUtils contained in commons-lang-2.4.jar to solve the problem, trasforming string in a multiline one
1 Like

Nice tip!

WordUtils really do the work. I still had some issue with Carriage returns so I’ve a little method for doing it :


private String formatMultiLineString( String text, int length )


String lines[] = text.split(“n”);

String result = “”;

for( int i = 0; i < lines.length; i ++ )


String line = lines;

if( i != 0 )

result += “n”;

result += WordUtils.wrap( line, length );


return result;



Problem Solved !

1 Like