Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT - Retrieve size of a widget that is not displayed

Tags:

gwt

I need to set the size of an absolutePanel regarding to its child size, but the getOffset* methods return 0 because (i think) the child has not been displayed yet.

A Quick example:

AbsolutePanel aPanel = new AbsolutePanel();
HTML text = new HTML(/*variable lenght text*/);
int xPosition = 20; // actually variable
aPanel.add(text, xPosition, 0);
aPanel.setSize(xPosition + text .getOffsetWidth() + "px", "50px"); // 20px 50px

I could also solve my problem by using the AbsolutePanel size to set the child position and size:

AbsolutePanel aPanel = new AbsolutePanel();
aPanel.setSize("100%", "50px");
HTML text = new HTML(/*variable lenght text*/);
int xPosition = aPanel.getOffsetWidth() / 3; // Once again, getOffsetWidth() returns 0;
 aPanel.add(text, xPosition, 0);

In both case, i have to find a way to either:

  • retrieve the size of a widget that has not been displayed
  • be notified when a widget is displayed
like image 392
Garagos Avatar asked Apr 22 '10 15:04

Garagos


1 Answers

Widget has an onLoad() method you can override that fires when the widget is attached to the DOM, at which point getOffsetWidth() will then return the rendered text size -- unless the widget your string is in already has some size applied. If you can do what you need with CSS (including maybe just setting "overflow: visible;") that's probably better, but sometimes you really need the size, and this technique will give it to you.

like image 132
phb Avatar answered Nov 15 '22 10:11

phb