Implementing textarea

What do you mean?

Can’t you just give it an id and get it from that like other elements?

Yep,



I can get it with getElementByName. But I get an instance of class Element, which doesn’t have any text manipulation methods?

That’s covered in the nifty manual. You can get the Renderer for an element and that then has the getters/setters/etc.

Ok, it works.



The text element even supports automatic line wrapping. Now one problem I have is: how can I make the scrollpanel expand when the text gets more lines than the area of the scrollpanel can display?

Assuming you mean that you add several Elements in a vertical layout, you need to set up the scroll panel control and relayout the contents as follows. The values in the setUp method can be adjusted to fit your context.

[java]ScrollPanel scrollPanel = getElement().findNiftyControl("scroll_panel_id_here", ScrollPanel.class);

scrollPanel.getElement().layoutElements();

scrollPanel.setUp(0, 5, 0, 50, AutoScroll.OFF);[/java]

Nope,



I don’t add more elements. I only use that one text element. It supports multiple lines and has automatic line break.

What are those numbers in the setup method?

You may well find you need to expand the height of the text panel accordingly - although quite how you can find out how many lines it is currently using I’m not sure. This may be a case where you need to find the text renderer source code and have a look at what it is doing…

In that case you may try just doing a textElement.getParent().layoutElements() to update the textElement layout.

The numbers in the setUp method have to do with scrolling increments (I’m not quite sure what each one is for).

Ok, as far as I understand the TextRenderer code, the getTextHeight() method should give me font.getHeight() * lineAmounts as height,

provided there is a textLineHeight set (not null).



So I set textLineHeight with:



[java]textRenderer.setTextLineHeight(new SizeValue(textRenderer.getFont().getHeight() + “px”));[/java]



After that I call getTextHeight() but getTextHeight only gives me the font height (height of one line). I don’t quite understand?

I solved it. I had to call setTextLineHeight before adding text.



But I still can’t get the scrollarea to expand. I made the panel inside the scrollpanel bigger with textPanel.setHeight(textHeight);

I also called textElement.getParent().layoutElements().



The scrollarea didnt expand. Any more suggestions?

Did you also call the setUp method of the scrollPanel?

I had to call setHeightConstraint instead of setHeight. It expanded now.

My hopefully last question.



How can I make the scrollbar of the scrollpanel scroll to the bottom when the text is expanded?



I called scrollPanel.setAutoscroll(AutoScroll.BOTTOM); but that FIXED the scrollbar at the bottom. It didn’t allow for scrolling anymore at all.



Then I called with a testnumber (10) scrollPanel.setVerticalPos();



But this apparently didn’t move the scrollbar. Any suggestions?



Also here is my control definition again:



[xml] <controlDefinition name=“textarea” controller=“gui.TextareaControl”>

<panel childLayout=“center” height="$height" width="$width" backgroundColor="$backgroundColor">

<control name=“scrollPanel” id=“textScrollpanel” horizontal=“false” height=“100%” width=“100%” backgroundColor="$backgroundColor">

<panel id=“textpanel” childLayout=“horizontal” backgroundColor="$backgroundColor" height=“100%” width=“100%”>

<text id=“areatext” style=“base-font” color="$textColor" width=“94%” />

</panel>

</control>

</panel>

</controlDefinition>

[/xml]



Can anyone tell me why the background of the textarea is always black? No matter what color I choose when adding it with <control name=“textarea” … backgroundColor="…"/>



My last question is: How can I create a text element from Java code?

I wouldn’t set both the control and the panel background colours as then transparency will get applied twice - set one or the other :slight_smile:



I’m not sure why it would always be black but I’d suggest doing some simple tests like for example removing the control and just having the panel with backgroundColour set and see if that shows, if it doesn’t try replacing the variable with a constant etc,…until you identify the actual step that isn’t working.

Got autoscroll to work.



Somehow he didn’t like backgroundColor as variable name. Changed it to textareaColor and now it works.



Does anyone know how I can create text elements from Java code?

There will be a builder for doing that - check the nifty manual under builders.

Yes, use TextBuilder (or LabelBuilder for labels).