Reputation: 4716
I'm trying to make a scroll panel that has relative size parameters. But the ScrollPanel.setSize(String, String)
function is impossible to work with if you have int values such as those returned by Window.getHeight
.
When I try ScrollPanel.setSize("100%", "150px");
it doesn't change the height of the scroll panel and instead uses the default. Any help would be appreciated.
Upvotes: 1
Views: 2252
Reputation: 1775
Copying my answer from here: How to use ScrollPanel with relative size
==================================================================
First, ScrollPanel is something not acting as other widget for reason I don't know why. Cannot set relative size (50%), and if I give it some style, it's lost somewhere and cannot be found from page.
My solution is to use a ResizeLayoutPanel. Andrei suggested using something ProvidesResize but requires the provide / require resize chain remain unbroken, which can be very tricky. I found this ResizeLayoutPanel "ProvidesResize to its one child, but does not RequiresResize", which is the perfect candidate for the root panel of my composite. Then, I just extend the ScrollPanel to resize itself in the "onResize" method whenever it's called by the parent ResizeLayoutPanel.
Still no answer to my first question: by ScrollPanel behave like this in the first place? I tried to find answer in its source code but was not successful (though I didn't spend enough time studying the source code).
public class My100PctScrollPanel extends ScrollPanel {
@Override
public void onResize() {
// Window.alert("on resize");
this.setWidth(this.getParent().getOffsetWidth()+"px");
this.setHeight(this.getParent().getOffsetHeight()+"px");
super.onResize();
}
}
........
compositeRoot = new ResizeLayoutPanel();
........
scrollPanel = new My100PctScrollPanel();
compositeRoot.clear();
compositeRoot.add(scrollPanel);
Upvotes: 0
Reputation: 1
I'm not sure if it's possible to do with the height, but what worked for me with the width was to first set an arbitrary width, then get the element and set the the width property.
ScrollPanel myScrollPanel = new ScrollPanel();
myScrollPanel.setSize("2112px", "150px"); // Arbitrary width.
myScrollPanel.getElement().getStyle().setProperty("width", "100%"); // or "auto"
Hope this works for you (although you asked this 5 years ago, so you're probably not too worried about it anymore)!
Upvotes: 0
Reputation: 946
According to the GWT doc for [ScrollPanel][1], they seem to be pretty strict about using the width in absolute and not relative CSS units. Thus the problem may be with the "100%" parameter.
[1]: http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/ScrollPanel.html#setSize(java.lang.String, java.lang.String)
Upvotes: 2