I'm attempting to resize a scene2d.ui window (which is basically a table that can be moved around) and its contents. So far I can only successfully change the window size itself, but not its contents. I'm doing it like this:
window.setWidth(newTableWidth);
textField.setWidth(newFieldWidth);
window.invalidate(); <--- New size of window is drawn properly when invalidate() is called
textField.invalidate(); <--- New textField size is NOT drawn, however
In this case, the window contains a text field which I'm trying to resize along with the window, but only the window gets redrawn with its new size while the text field remains the same.
How do I properly update the widgets within the window?
Based on BeenX's answer, it's evident that the size is set on the cell of the table that contains the widget, rather than on the widget itself. The table.add()
method returns a cell, so when running table.add(textField).width(600).height(60)
the width and height is being applied to this cell.
Hence my solution to update an already existing cell was to find the cell I want to update with table.getCells()
then apply the new width on that. The example below changes the width of the last cell in the list.
List<Cell> cells = window.getCells();
Cell cell = cells.get(cells.size()-1);
cell.width(newWidth);
I had the same Problem and it took a while to get the solution. Take a look at this quastion slider always has default width.
The solution is:
UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.
Layout at Wiki
From the wiki of libGDX. They just added it. So while you add those widgets to the table just add the line .width(newTableWidth)
at the end and it should be at the right width.
table.add(textField).width(600).height(60);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With