Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the size of an existing scene2d.ui widget in a window/table?

Tags:

libgdx

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?

like image 603
mattboy Avatar asked Aug 30 '13 18:08

mattboy


2 Answers

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);
like image 97
mattboy Avatar answered Oct 02 '22 17:10

mattboy


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);
like image 42
BennX Avatar answered Oct 02 '22 16:10

BennX