GWT - Retrieve size of a widget that is not displayed

不打扰是莪最后的温柔 提交于 2020-03-13 07:58:14

问题


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

回答1:


I think you should try to solve this with css styles. If I were you, I would try to set a style for parent and/or absolute panel, using addStyle or addStyleNames. Maybe something like...

AbsolutePanel aPanel = new AbsolutePanel();
aPanel.setSize("100%", "50px");
HTML text = new HTML(/*variable lenght text*/);
text.addStyleName("my-custom-style");

in css you would create a css style which would then size your widget appropriately.

The other approach would be....

You could create a function that creates a HTML widget exactly the size you need.

Here's some code for you, to get you started:

public native void customizeHTMLElement(Element elem, String widthInPx) /*-{ 
  elem.style.width = widthInPx;   //this is basically pure js. 
}-*/ 

Then somewhere within your widget make a call to the above function like

public void foo() {
  ...
  customizeHTMLElement(someElement, "20");
  ...
}

Some info about JSNI - http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html




回答2:


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.




回答3:


You should call getOffsetHeight() only after widget will be placed and calculated. So GWT have

com.google.gwt.core.client.Scheduler

class to do this.

In my project I use LayoutPanel to show menu at top and other content at center. But I don't know the size of menu. So I provide information to LayoutPanel calculated using getOffsetHeight like this:

layoutPanel.add(widget);
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
{
    public void execute()
    {
        int offsetHeight = widget.getOffsetHeight();
        layoutPanel.setWidgetTopHeight(widget, 0, Style.Unit.PX, offsetHeight, Style.Unit.PX);
    }
});


来源:https://stackoverflow.com/questions/2692097/gwt-retrieve-size-of-a-widget-that-is-not-displayed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!