Unable to change the size of the content of a scrollPanel

Hi all,



I am developing a game in which I need to dynamically add some content to a scrollPanel. The content is correctly added but the scrollbar remains unchanged so I cannot go down to the lower data added. If the panel inside the scrollpanel in the xml is empty and I add all the data at the initialization (in the niftGui bind method) the scrollpanel resizes correctly.



I have made a simple test case which behaves the same way. I copy the code of the sample.



my nifty xml (based on the nigtyGUI sample) file looks like:

[xml]

…

<control id="scroll" name="scrollPanel" style="special-scrollpanel" horizontal="false" stepSizeY="15" >

<panel id="panel1" childLayout="center" width="100%">

<panel id="panel2" childLayout="vertical" width="100%">

<!-- label to interact with -->

<label id="label1" text="label" style="menu-item">

<interact onClick="addItem(asdf)" />

</label>

</panel>

</panel>

</control>

…

[/xml]



This is the method I use yo add new items in the scrollpanel:



[java]

public void addItem(String name)

{

//internal panel (to add data)

Element panel2=niftyDisplay.getNifty().getCurrentScreen().findElementByName("panel2");

//external panel (the one that defines the size in the sample)

Element panel1=niftyDisplay.getNifty().getCurrentScreen().findElementByName("panel1");

int newHeight=(panel1!=null)?panel1.getHeight():0;



LabelCreator lblCreator=new LabelCreator("");

lblCreator.setId(name);

lblCreator.setText(name);

lblCreator.setStyle("menu-item");

lblCreator.setInteractOnClick("addItem(" + (new Random()).nextInt()) + ")");

lblCreator.create(niftyDisplay.getNifty(), niftyDisplay.getNifty().getCurrentScreen(), panel2);



panel2.layoutElements();



newHeight+=50; //because of font size

panel1.setConstraintHeight(new SizeValue( newHeight + "px"));

panel1.layoutElements();

}

[/java]



I have also tried to layoutElements on parent elements (scrollpanel, main panel in the layer) and layoutLayers on the screen.



Is there any solution to make this work or other better way to implement dynamic content scrolls?



Thank you all!

Hello,



sorry to bother again, but… no one is able to help me with this? :S



Thanks :wink:

Well, I think calling scrollPanel.setUp(final float stepSizeX, final float stepSizeY, final float pageSizeX, final float pageSizeY, final AutoScroll auto); after you’ve added the content should update the scrollbar correctly.



The problem is that the scrollbars are not updated automatically currently so you’ll need to trigger that manually which that “setUp” method should do.

Thanks for the comment @void256. I don’t have the “setUp” method in ScrollPanel class (probably because I’m still using jme3 alpha3), but using “scrollPanel.initializeScrollPanel(nifty.getCurrentScreen(), 0, 15);” the scrollbar is correctly updated.

Hey, dragging this up because I have this problem too.

I do have the setUp() method @void256 suggested but it doesn’t seem to have any effect at all on my ScrollPanel contents and I don’t have the initializeScrollPanel() method. I’m using standard Alpha 3.0RC2.

I have a label control nested in my ScrollPanel and I’m adding lines to the label, making it’s height bigger and then trying to update the ScrollPanel somehow but absolutely nothing I have tried has had any effect at all on the scroll bar or or panel contents.

Any suggestions? Thanks

I spent most of a day recently fighting the nifty scroll panel and just could not get the behaviour I needed. In the end I used two nested panels and a scroll bar control to create my own scrolling area and that worked perfectly…

Create panel viewPort - layout absolute, clipping true
Create panel contents - layout whatever, inside viewport
Create control scrollbar outside the viewport but adjacent to it.

After adding/removing from the contents panel and calling relayout in it set a “recalculateScrollbar” flag. (This is needed to make sure things like removing elements etc is processed and height adjusted before updating the scrollbar)

In update if recalculateScrollbar is set then set the scrollbar worldMax to contents.getHeight(), you also need to update worldPageSize to viewPort.getHeight whenever you set worldMax
When the scrollbar is dragged, set contents constraintY to -value() px and then relayout viewport

And there you have it, your own scrollpane. Depending on desired behaviour you may want to add some code to cope with contents being smaller than viewport, autoscroll, whatever - or add horizontal scrollbars working the same way but modifying constraintX as well as Y, but this gives you a functional starting point.

1 Like